Skip to content

Commit

Permalink
Add support for bare type
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Aug 14, 2022
1 parent c5d57b7 commit 8da9307
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/4375-hramezani.md
@@ -0,0 +1 @@
Add support for bare `type`
3 changes: 3 additions & 0 deletions pydantic/typing.py
Expand Up @@ -568,6 +568,9 @@ def get_class(type_: Type[Any]) -> Union[None, bool, Type[Any]]:
Tries to get the class of a Type[T] annotation. Returns True if Type is used
without brackets. Otherwise returns None.
"""
if type_ is type:
return True

if get_origin(type_) is None:
return None

Expand Down
10 changes: 6 additions & 4 deletions tests/test_main.py
Expand Up @@ -981,18 +981,20 @@ class ArbitraryClassAllowedModel(BaseModel):
]


def test_bare_type_type_validation_success():
@pytest.mark.parametrize('bare_type', [type, Type])
def test_bare_type_type_validation_success(bare_type):
class ArbitraryClassAllowedModel(BaseModel):
t: Type
t: bare_type

arbitrary_type_class = ArbitraryType
m = ArbitraryClassAllowedModel(t=arbitrary_type_class)
assert m.t == arbitrary_type_class


def test_bare_type_type_validation_fails():
@pytest.mark.parametrize('bare_type', [type, Type])
def test_bare_type_type_validation_fails(bare_type):
class ArbitraryClassAllowedModel(BaseModel):
t: Type
t: bare_type

arbitrary_type = ArbitraryType()
with pytest.raises(ValidationError) as exc_info:
Expand Down

0 comments on commit 8da9307

Please sign in to comment.