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

fix(mypy): fix custom Path and UUID related types #2420

Merged
merged 4 commits into from Mar 2, 2021
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/2418-PrettyWood.md
@@ -0,0 +1 @@
fix `mypy` complaints on `Path` and `UUID` related custom types
8 changes: 4 additions & 4 deletions pydantic/_hypothesis_plugin.py
Expand Up @@ -139,10 +139,10 @@ def add_luhn_digit(card_number: str) -> str:
)

# UUIDs
st.register_type_strategy(pydantic.UUID1, st.uuids(version=1)) # type: ignore[arg-type]
st.register_type_strategy(pydantic.UUID3, st.uuids(version=3)) # type: ignore[arg-type]
st.register_type_strategy(pydantic.UUID4, st.uuids(version=4)) # type: ignore[arg-type]
st.register_type_strategy(pydantic.UUID5, st.uuids(version=5)) # type: ignore[arg-type]
st.register_type_strategy(pydantic.UUID1, st.uuids(version=1))
st.register_type_strategy(pydantic.UUID3, st.uuids(version=3))
st.register_type_strategy(pydantic.UUID4, st.uuids(version=4))
st.register_type_strategy(pydantic.UUID5, st.uuids(version=5))

# Secrets
st.register_type_strategy(pydantic.SecretBytes, st.binary().map(pydantic.SecretBytes))
Expand Down
14 changes: 6 additions & 8 deletions pydantic/types.py
Expand Up @@ -517,8 +517,6 @@ def conlist(item_type: Type[T], *, min_items: int = None, max_items: int = None)


if TYPE_CHECKING:
# TODO: add `str` and support it thanks to the plugin
# PyObject = Union[str, Callable[..., Any]]
PyObject = Callable[..., Any]
else:

Expand Down Expand Up @@ -632,10 +630,10 @@ def condecimal(
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ UUID TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if TYPE_CHECKING:
UUID1 = Union[UUID, str]
UUID3 = Union[UUID, str]
UUID4 = Union[UUID, str]
UUID5 = Union[UUID, str]
UUID1 = UUID
UUID3 = UUID
UUID4 = UUID
UUID5 = UUID
else:

class UUID1(UUID):
Expand All @@ -658,8 +656,8 @@ class UUID5(UUID1):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PATH TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if TYPE_CHECKING:
FilePath = Union[Path, str]
DirectoryPath = Union[Path, str]
FilePath = Path
DirectoryPath = Path
else:

class FilePath(Path):
Expand Down
26 changes: 20 additions & 6 deletions tests/mypy/modules/success.py
Expand Up @@ -208,15 +208,29 @@ class PydanticTypes(BaseModel):
# String
my_strict_str: StrictStr = 'pika'
# PyObject
# TODO: my_pyobject_str: PyObject = 'datetime.date'
my_pyobject_str: PyObject = 'datetime.date' # type: ignore
my_pyobject_callable: PyObject = date
# UUID
my_uuid1: UUID1 = UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
my_uuid1_str: UUID1 = 'a8098c1a-f86e-11da-bd1a-00112444be1e'
my_uuid1_str: UUID1 = 'a8098c1a-f86e-11da-bd1a-00112444be1e' # type: ignore
# Path
my_file_path: FilePath = Path('root') / 'myfile.txt'
my_file_path_str: FilePath = 'root/myfile.txt'
my_dir_path: DirectoryPath = Path('root') / 'mydir'
my_dir_path_str: DirectoryPath = 'root/mydir'
my_file_path: FilePath = Path(__file__)
my_file_path_str: FilePath = __file__ # type: ignore
my_dir_path: DirectoryPath = Path('.')
my_dir_path_str: DirectoryPath = '.' # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one day, when we solve koxudaxi/pydantic-pycharm-plugin#36 (comment) we could update the mypy plugin to avoid the need for all these # type: ignores

# Json
my_json: Json = '{"hello": "world"}'

class Config:
validate_all = True


validated = PydanticTypes()
validated.my_pyobject_str(2021, 1, 1)
validated.my_pyobject_callable(2021, 1, 1)
validated.my_uuid1.hex
validated.my_uuid1_str.hex
validated.my_file_path.absolute()
validated.my_file_path_str.absolute()
validated.my_dir_path.absolute()
validated.my_dir_path_str.absolute()