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

pydantic field_validators should be @classmethods #111

Closed
sneakers-the-rat opened this issue Jul 9, 2024 · 2 comments · Fixed by #112
Closed

pydantic field_validators should be @classmethods #111

sneakers-the-rat opened this issue Jul 9, 2024 · 2 comments · Fixed by #112

Comments

@sneakers-the-rat
Copy link
Contributor

pydantic docs read:

@field_validators are "class methods", so the first argument value they receive is the UserModel class, not an instance of UserModel. We recommend you use the @classmethod decorator on them below the @field_validator decorator to get proper type checking.

Specifically here:

importing with pydantic 2.8 yields:

  File "python3.11/site-packages/linkml_runtime/__init__.py", line 3, in <module>
    from linkml_runtime.utils.schemaview import SchemaView
  File "python3.11/site-packages/linkml_runtime/utils/schemaview.py", line 12, in <module>
    from linkml_runtime.utils.namespaces import Namespaces
  File "python3.11/site-packages/linkml_runtime/utils/namespaces.py", line 8, in <module>
    from prefixmaps.io.parser import load_context
  File "python3.11/site-packages/prefixmaps/__init__.py", line 1, in <module>
    from .datamodel.context import Context, PrefixExpansion, StatusType
  File "python3.11/site-packages/prefixmaps/datamodel/context.py", line 10, in <module>
    import curies
  File "python3.11/site-packages/curies/__init__.py", line 5, in <module>
    from .api import (
  File "/python3.11/site-packages/curies/api.py", line 252, in <module>
    class Record(BaseModel):  # type:ignore
  File "python3.11/site-packages/pydantic/_internal/_model_construction.py", line 96, in __new__
    private_attributes = inspect_namespace(
                         ^^^^^^^^^^^^^^^^^^
  File "python3.11/site-packages/pydantic/_internal/_model_construction.py", line 401, in inspect_namespace
    raise PydanticUserError(
pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `prefix_not_in_synonyms = pydantic.functional_validators.field_validator`. All model fields require a type annotation; if `prefix_not_in_synonyms` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.

For further information visit https://errors.pydantic.dev/2.8/u/model-field-missing-annotation

simple fix - add @classmethod underneath

@sneakers-the-rat
Copy link
Contributor Author

the dependency spec is too generous re: pydantic version too -

pydantic

changes like these add up, and the package is no longer compatible with pydantic v1 ;) - def recommend dropping support, it's been more than a year

cthoyt added a commit that referenced this issue Jul 9, 2024
Fixes #111

Co-authored-by: Charles Tapley Hoyt <[email protected]>
cthoyt added a commit that referenced this issue Jul 9, 2024
@cthoyt
Copy link
Member

cthoyt commented Jul 9, 2024

Thanks @sneakers-the-rat for investigating this and sending a fix. I will make a new release now, but have also added a pending deprecation in b8d500a for the end of October coincident with dropping py38 support.

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