Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using types-jsonschema 4.4.0, mypy does not consider jsonschema.validators.Draft7Validator a type #7028

Closed
DMRobertson opened this issue Jan 24, 2022 · 2 comments · Fixed by #7035

Comments

@DMRobertson
Copy link
Contributor

DMRobertson commented Jan 24, 2022

(I'm not sure if this is best filed as an issue against typeshed or mypy. Happy to move this if it's the latter.)

The snippet

import jsonschema
x: jsonschema.Draft7Validator

does not typecheck on with types-jsonschema 4.4.0.

error: Variable "jsonschema.validators.Draft7Validator" is not valid as a type  [valid-type]

For cross referencing, we noticed this here.

The snippet above does type-check with 4.3.0, where Draft7Validator was annotated as Any. The change was made in #7025 .

Are we misunderstanding or misusing Draft7Validator - is it not expected to be a type like this? If not, perhaps this is a problem in mypy rather than the stubs?

For what it's worth: mypy's documentation suggests that tp: Type[_Validator] is considered a "variable of type _Validator". Perhaps mypy's complaint is that tp's value could be a replaced with a different subtype of _Validator at runtime? With that in mind, I tried patching the stubs to read Draft7Validator: Final[type[_Validator]] but this didn't seem to appease the type gods.

@DMRobertson
Copy link
Contributor Author

Come to think of it, maybe Draft7Validator = NewType("Draft7Validator", _Validator) would do the trick?

@Akuli
Copy link
Collaborator

Akuli commented Jan 24, 2022

We should probably just use empty classes with _Validator as a base class, as in class Draft7Validator(_Validator): .... At runtime, jsonschema has a function that creates a new validator class, and making new classes in typeshed is the closest we can get to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants