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

* 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
This commit is contained in:
Eric Jolibois
2021-03-02 13:07:07 +01:00
committed by GitHub
parent b2d3f333f0
commit 37c37fd55e
4 changed files with 31 additions and 18 deletions
+1
View File
@@ -0,0 +1 @@
fix `mypy` complaints on `Path` and `UUID` related custom types
+4 -4
View File
@@ -139,10 +139,10 @@ st.register_type_strategy(
)
# 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))
+6 -8
View File
@@ -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 @@ else:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PATH TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if TYPE_CHECKING:
FilePath = Union[Path, str]
DirectoryPath = Union[Path, str]
FilePath = Path
DirectoryPath = Path
else:
class FilePath(Path):
+20 -6
View File
@@ -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()