Skip to content

Commit

Permalink
fix(mypy): fix custom Path and UUID related types (#2420)
Browse files Browse the repository at this point in the history
* add tests that should pass

But we have those errors

226: error: Item "str" of "Union[UUID, str]" has no attribute "hex"  [union-attr]
227: error: Item "str" of "Union[UUID, str]" has no attribute "hex"  [union-attr]
228: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
229: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
230: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
231: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]

* fix: right types should be valid

* remove new useless `type: ignore`

* docs: add change file
  • Loading branch information
PrettyWood committed Mar 2, 2021
1 parent b2d3f33 commit 37c37fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
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
# 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()

0 comments on commit 37c37fd

Please sign in to comment.