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

Add support for bare type #4375

Merged
merged 1 commit into from Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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