Skip to content

Variable "pydantic_core.core_schema.FieldValidationInfo" is not valid as a type #994

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

Closed
sisp opened this issue Sep 28, 2023 · 6 comments · Fixed by #995
Closed

Variable "pydantic_core.core_schema.FieldValidationInfo" is not valid as a type #994

sisp opened this issue Sep 28, 2023 · 6 comments · Fixed by #995
Assignees

Comments

@sisp
Copy link
Contributor

sisp commented Sep 28, 2023

The deprecation of pydantic_core.core_schema.FieldValidationInfo in v2.10.0 makes mypy unhappy:

from pydantic_core.core_schema import FieldValidationInfo

def f(x: FieldValidationInfo):
    ...
$ mypy test.py
test.py:3: error: Variable "pydantic_core.core_schema.FieldValidationInfo" is not valid as a type  [valid-type]
test.py:3: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

No error occurs when using v2.9.0.

This behavior turns the deprecation into a breaking change (at least regarding type-checking). As a result, we can't update from Pydantic v2.3 to v2.4 as pydantic-core is a pinned dependency and bumped to v2.10.

@dmontagu
Copy link
Collaborator

@sisp The import name was just changed (with a fallback to keep older code working). From pydantic_core.core_schema:

_deprecated_import_lookup = {
    'FieldValidationInfo': ValidationInfo,
    'FieldValidatorFunction': WithInfoValidatorFunction,
    'GeneralValidatorFunction': WithInfoValidatorFunction,
    'FieldWrapValidatorFunction': WithInfoWrapValidatorFunction,
}

I think if you change the import to ValidationInfo instead of FieldValidationInfo the mypy error will go away.

@adriangb
Copy link
Member

Also see pydantic/pydantic#7667

@sisp
Copy link
Contributor Author

sisp commented Sep 28, 2023

True, I still think a deprecation should not break untouched code, not even for type-checking. Is there a way to fix the original import to keep mypy happy?

@dmontagu
Copy link
Collaborator

I'm not taking a side in the "mypy errors are breaking changes" argument, but for what it's worth, you could add an if TYPE_CHECKING: block for mypy to see the import while still ensuring that it is importable with the deprecation warning (thanks to the getattr).

@adriangb
Copy link
Member

I agree that might be the fix. If it works a PR to implement it would be welcome.

@sisp
Copy link
Contributor Author

sisp commented Sep 29, 2023

I've submitted a PR with the suggested fix: #995

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

Successfully merging a pull request may close this issue.

4 participants