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

Variable "pydantic.FieldValidationInfo" is not valid as a type #7667

Closed
1 task done
LachlanMarnham opened this issue Sep 27, 2023 · 9 comments
Closed
1 task done

Variable "pydantic.FieldValidationInfo" is not valid as a type #7667

LachlanMarnham opened this issue Sep 27, 2023 · 9 comments

Comments

@LachlanMarnham
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

There seems to be a breaking change in v2.4. When I run mypy against my code I see:

error: Variable "pydantic.FieldValidationInfo" is not valid as a type  [valid-type]
note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
error: pydantic.FieldValidationInfo? has no attribute "field_name"  [attr-defined]
error: Variable "pydantic.FieldValidationInfo" is not valid as a type  [valid-type]
note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
error: pydantic.FieldValidationInfo? has no attribute "data"  [attr-defined]

This worked in pydantic==2.3.0 (confirmed with downgrade).

In the 2.3 version of the migration docs, FieldValidationInfo is the recommended way to type the info parameter to field_validators. However this has been removed from the 2.4 version of the migration docs.

In v2.4, I can see this in pydantic/__init__.py:

# FieldValidationInfo is deprecated, and hidden behind module a `__getattr__`
'FieldValidationInfo': ('pydantic_core', '.core_schema'),

but this is missing from v2.3. Is this an intentional breaking change to a minor release? I can't see anything about it in the changelog.

Note: I think this is only broken in the pydantic.mypy plugin, because FieldValidationInfo is importable, and it does have the field_name and data attributes which mypy thinks are missing.

Possibly relates to #7617

Many thanks.

Example Code

# file.py
from pydantic import BaseModel, FieldValidationInfo, field_validator


class Model(BaseModel):
    x: int

    @field_validator('x')
    def val_x(cls, v: int, info: FieldValidationInfo) -> int:
        assert info.config is not None
        print(info.config.get('title'))
        #> Model
        print(cls.model_fields[info.field_name].is_required())
        #> True
        return v


Model(x=1)

Running mypy file.py with the pydantic.mypy plugin enabled results in:

Variable "pydantic.FieldValidationInfo" is not valid as a type  [valid-type]
note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
error: FieldValidationInfo? has no attribute "config"  [attr-defined]
error: FieldValidationInfo? has no attribute "data"  [attr-defined]
error: FieldValidationInfo? has no attribute "config"  [attr-defined]
error: FieldValidationInfo? has no attribute "field_name"  [attr-defined]
Found 5 errors in 1 file (checked 1 source file)


### Python, Pydantic & OS Version

```Text
python version: 3.11
pydantic version: 3.4.1
os version: Windows 10 Enterprise
@LachlanMarnham LachlanMarnham added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 27, 2023
@hramezani
Copy link
Member

Thanks @LachlanMarnham for reporting this 🙏

I couldn't reproduce the problem.

Could you please share your:

  • pydantic version
  • pydantic-core version
  • mypy version
  • mypy config

@hramezani hramezani removed the unconfirmed Bug not yet confirmed as valid/applicable label Sep 27, 2023
@Parici75
Copy link

Parici75 commented Sep 27, 2023

@hramezani,

Same problem here upon upgrading to Pydantic v2.4.

The validators stays functional, but mypy complains with :
error: Variable "pydantic_core.core_schema.FieldValidationInfo" is not valid as a type [valid-type]
and
error: FieldValidationInfo? has no attribute "data" [attr-defined]

My configuration :

  • pydantic=2.4.1
  • pydantic-core=2.10.1
  • mypy=1.5.1

And my mypy config :

[tool.mypy]
python_version = 3.11
allow_untyped_defs = false
exclude = ["tests"]
plugins = [
  "pydantic.mypy", "numpy.typing.mypy_plugin"
]
follow_imports = "silent"
warn_redundant_casts = true
warn_unused_ignores = true
disallow_any_generics = true
no_implicit_reexport = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = false
warn_required_dynamic_aliases = true

@peterHoburg
Copy link
Contributor

I am having the same issue, but with Pyright.

Pydantic 2.4.1
pydantic-core 2.10.1
pyright 1.1.238

Error is the same error: Expected type expression but received "object"

@hramezani
Copy link
Member

please replace FieldValidationInfo with ValidationInfo. As FieldValidationInfo is deprecated.

you can import it by from pydantic import ValidationInfo

@peterHoburg
Copy link
Contributor

There was no indication in the patch notes that FieldValidationInfo was being deprecated, the 2.4 docs now do not contain FieldValidationInfo, and the migration docs were updated without notes.

In previous patch notes this has been called out explicitly, and the assumption is it would be called out in the future. The ValidationInfo docs should also state that it is the replacement for FieldValidationInfo, with a migration path.

@hramezani
Copy link
Member

Sorry @peterHoburg for that.

It was actually because Allow access to field_name and data in all validators

You can find it's documentation here

@peterHoburg
Copy link
Contributor

Sorry @peterHoburg for that.

It was actually because Allow access to field_name and data in all validators

You can find it's documentation here

Thanks for the reply! That makes complete sense. I opened a PR here to add a small note in the docs saying FieldValidationInfo is deprecated. I was not sure what version it was going to be removed in, so I did not include that.

#7670

@adriangb
Copy link
Member

Apologies for any breakages. We did everything we could to avoid this being a breaking change (hence the deprecation warning and not just removal, putting it behind __getattr__, etc. I actually wanted to just remove it but I'm glad @samuelcolvin convinced me otherwise, clearly he's experienced the nuances of working with a project as popular as Pydantic longer than I have.

The core of the matter is that try as we might we may to avoid any sort of breaking changes with the breadth of usage Pydantic gets almost any change has the potential to break something for someone in some way. In this case we probably didn't consider the implications for type checkers. Luckily like Hasan said above the fix should be as simple as replacing an import.

@LachlanMarnham
Copy link
Author

Hey guys thanks for the replies. After seeing the deprecation warning I was pretty sure the removal was intentional, mostly raised the issue because of the missing changelog notes and for others hitting the same problem who come to GitHub issues to find solutions. From my POV I'm happy for this to be closed now that #7670 has been merged. The bug v2 tag can probably also be removed, but I'll leave that to you @hramezani.

Thanks again.

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

No branches or pull requests

6 participants