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

Issue with pydantic's newly enforced field name check #720

Closed
4ch1m opened this issue Jul 2, 2023 · 4 comments
Closed

Issue with pydantic's newly enforced field name check #720

4ch1m opened this issue Jul 2, 2023 · 4 comments

Comments

@4ch1m
Copy link
Contributor

4ch1m commented Jul 2, 2023

Hi there,

I'm experiencing issues when using the pytradfri library in an app with Gunicorn.

However, the issue seems to be related to pydantic in first place.

I get this error while bootstrapping:

  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 171, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 361, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_fields.py", line 112, in collect_model_fields
    raise NameError(f'Field "{ann_name}" has conflict with protected namespace "{protected_namespace}"')
NameError: Field "model_number" has conflict with protected namespace "model_"

The root cause seems to be this class:

model_number: str = Field(alias=ATTR_DEVICE_MODEL_NUMBER)

Having field names prefixed with model seems to be discouraged now:

All methods on models will start with model_, fields' names will not be allowed to start with "model" (aliases can be used if required).

(see: Model Namespace Cleanup)

Not sure how/if this could/should be fixed in the pytradfri library though.

Thanks for reading. 😄

@MartinHjelmare
Copy link
Contributor

Pydantic 2.0 has new restrictions on attribute names. You can work around it for now by pinning pydantic to a version < 2.

We'll have to think about how to handle this here in the library.

@4ch1m
Copy link
Contributor Author

4ch1m commented Jul 2, 2023

All right. Thanks for the feedback. 👍

@ggravlingen
Copy link
Member

I did an upgrade to 2.0 in another library and apparently, in order to be able to transition smoothly into the new version, you can selectively import from v1 of the library under v2. This is a potential way to move forward and keep the current field name.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
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

3 participants