diff --git a/changes/2418-PrettyWood.md b/changes/2418-PrettyWood.md new file mode 100644 index 0000000000..cceb189661 --- /dev/null +++ b/changes/2418-PrettyWood.md @@ -0,0 +1 @@ +fix `mypy` complaints on `Path` and `UUID` related custom types diff --git a/pydantic/_hypothesis_plugin.py b/pydantic/_hypothesis_plugin.py index 0da35558f0..588e537646 100644 --- a/pydantic/_hypothesis_plugin.py +++ b/pydantic/_hypothesis_plugin.py @@ -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)) diff --git a/pydantic/types.py b/pydantic/types.py index 06220f771a..2e4eb284e3 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -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: @@ -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): @@ -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): diff --git a/tests/mypy/modules/success.py b/tests/mypy/modules/success.py index b90abdede1..4d8d0c1411 100644 --- a/tests/mypy/modules/success.py +++ b/tests/mypy/modules/success.py @@ -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 # 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()