Skip to content

Commit

Permalink
Make it work to use None as a generic parameter (#6609)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Jul 12, 2023
1 parent bddc5ba commit 1e50b0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -702,8 +702,6 @@ def _generate_dc_field_schema(
)

def _common_field_schema(self, name: str, field_info: FieldInfo, decorators: DecoratorInfos) -> _CommonField:
assert field_info.annotation is not None, 'field_info.annotation should not be None when generating a schema'

source_type, annotations = field_info.annotation, field_info.metadata

def set_discriminator(schema: CoreSchema) -> CoreSchema:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_generics.py
Expand Up @@ -2477,3 +2477,13 @@ class Model(BaseModel):
Model(f_value=(1, 'abc'))
with pytest.raises(ValidationError):
Model(f_value=('abc', True))


def test_generic_none():
T = TypeVar('T')

class Container(BaseModel, Generic[T]):
value: T

assert Container[type(None)](value=None).value is None
assert Container[None](value=None).value is None

0 comments on commit 1e50b0d

Please sign in to comment.