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

Support re.Pattern and bare type as types #4360

Closed
3 of 14 tasks
Bobronium opened this issue Aug 9, 2022 · 1 comment · Fixed by #4375
Closed
3 of 14 tasks

Support re.Pattern and bare type as types #4360

Bobronium opened this issue Aug 9, 2022 · 1 comment · Fixed by #4375

Comments

@Bobronium
Copy link
Contributor

Bobronium commented Aug 9, 2022

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Ref: #4334 (comment)

import re
import typing
from pydantic import BaseModel

class Model(BaseModel):
    typing_pattern: typing.Pattern  # this is supported
    re_pattern: re.Pattern  # RuntimeError: no validator found for <class 're.Pattern'>, see `arbitrary_types_allowed` in Config
from typing import Type
from pydantic import BaseModel

class Model(BaseModel):
    cls: Type  # treated as Type[Any]
    cls: type  # RuntimeError: no validator found for <class 'type'>, see `arbitrary_types_allowed` in Config

It's pretty easy to fix, but my hands are full now. Would appreciate somebody picking this up.

Would require adding or self.type_ is re.Pattern here:

elif self.type_ is Pattern:

adding or type_ is re.Pattern here:

if type_ is Pattern:

and replacing part after or with field_type is type_ and (type_ is Pattern or type_ is re.Pattern)

if lenient_issubclass(field_type, type_) or field_type is type_ is Pattern:

As for bare type, you'd need to add if type_ is type: return True before this line:

if get_origin(type_) is None:

Affected Components

@hramezani
Copy link
Member

I've created a PR to add re.Pattern #4366
I will create another one for type soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants