-
-
Notifications
You must be signed in to change notification settings - Fork 367
fix: change None only field types to Literal[None] #1252
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
base: main
Are you sure you want to change the base?
fix: change None only field types to Literal[None] #1252
Conversation
This is to fix the error from Pydantic v2.12+ like the one below: ```python .nox/test/lib/python3.14/site-packages/supabase/__init__.py:1: in <module> from supabase_auth.errors import ( .nox/test/lib/python3.14/site-packages/supabase_auth/__init__.py:3: in <module> from ._async.gotrue_admin_api import AsyncGoTrueAdminAPI # type: ignore # noqa: F401 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .nox/test/lib/python3.14/site-packages/supabase_auth/_async/gotrue_admin_api.py:6: in <module> from ..helpers import ( .nox/test/lib/python3.14/site-packages/supabase_auth/helpers.py:27: in <module> from .types import ( .nox/test/lib/python3.14/site-packages/supabase_auth/types.py:104: in <module> class AuthOtpResponse(BaseModel): .nox/test/lib/python3.14/site-packages/pydantic/_internal/_model_construction.py:242: in __new__ set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver) .nox/test/lib/python3.14/site-packages/pydantic/_internal/_model_construction.py:566: in set_model_fields fields, class_vars = collect_model_fields(cls, config_wrapper, ns_resolver, typevars_map=typevars_map) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .nox/test/lib/python3.14/site-packages/pydantic/_internal/_fields.py:364: in collect_model_fields field_info = FieldInfo_.from_annotated_attribute(ann_type, assigned_value, _source=AnnotationSource.CLASS) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .nox/test/lib/python3.14/site-packages/pydantic/fields.py:382: in from_annotated_attribute raise PydanticUserError( E pydantic.errors.PydanticUserError: Error when building FieldInfo from annotated attribute. Make sure you don't have any field name clashing with a type annotation. E E For further information visit https://errors.pydantic.dev/2.12/u/unevaluable-type-annotation ```
I cannot reproduce this error, even after updating pydantic to 2.12.0 and python to 3.14.0-candidate-2. Additionally, the error you reported, by what is described in the pydantic reference link in the error message, is about field names and annotations having the same name, not annotations and default values. Thus, I don't think that changing
Can you explain how to reproduce this? |
Okay, I can reproduce this by dropping into a shell and running |
I've opened issue pydantic/pydantic#12368, let's see what they say before changing this behavior ourselves. If it indeed should be a bug, then I'll quickly merge this. |
Indeed, this seems to be a bug with pydantic itself pydantic/pydantic#12370. I'll keep this PR open until we're sure it is resolved upstream. |
This is to fix the error from Pydantic v2.12+ like the one below:
What kind of change does this PR introduce?
Bug fix that started showing after upgrading Pydantic to v2.12.0 as part of the chore to upgrade Python to 3.14
What is the current behavior?
Any imports, even if they are not related to Auth classes, fail with the error seen above. This is because the check in Pydantic looks like this:
Here, the check is that the
annotation
(which isNone
) is the same as the default value, which is alsoNone
.What is the new behavior?
The change is to use
Literal[None]
as theannotation
on the field, while maintaining thedefault
asNone
. It then makes the checkannotation is default
evaluate toFalse
.Additional context
None