diff --git a/Makefile b/Makefile index bc2a1525..545b70d9 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ clean-test: ## remove test and coverage artifacts rm -fr htmlcov/ lint: ## check style with ruff and black - pdm run ruff src/ tests bench + pdm run ruff check src/ tests bench pdm run black --check src tests docs/conf.py test: ## run tests quickly with the default Python diff --git a/pdm.lock b/pdm.lock index 809b6d39..8d170ae5 100644 --- a/pdm.lock +++ b/pdm.lock @@ -1165,27 +1165,28 @@ files = [ [[package]] name = "ruff" -version = "0.1.13" +version = "0.9.1" requires_python = ">=3.7" summary = "An extremely fast Python linter and code formatter, written in Rust." files = [ - {file = "ruff-0.1.13-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:e3fd36e0d48aeac672aa850045e784673449ce619afc12823ea7868fcc41d8ba"}, - {file = "ruff-0.1.13-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:9fb6b3b86450d4ec6a6732f9f60c4406061b6851c4b29f944f8c9d91c3611c7a"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b13ba5d7156daaf3fd08b6b993360a96060500aca7e307d95ecbc5bb47a69296"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9ebb40442f7b531e136d334ef0851412410061e65d61ca8ce90d894a094feb22"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:226b517f42d59a543d6383cfe03cccf0091e3e0ed1b856c6824be03d2a75d3b6"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:5f0312ba1061e9b8c724e9a702d3c8621e3c6e6c2c9bd862550ab2951ac75c16"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2f59bcf5217c661254bd6bc42d65a6fd1a8b80c48763cb5c2293295babd945dd"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e6894b00495e00c27b6ba61af1fc666f17de6140345e5ef27dd6e08fb987259d"}, - {file = "ruff-0.1.13-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9a1600942485c6e66119da294c6294856b5c86fd6df591ce293e4a4cc8e72989"}, - {file = "ruff-0.1.13-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:ee3febce7863e231a467f90e681d3d89210b900d49ce88723ce052c8761be8c7"}, - {file = "ruff-0.1.13-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:dcaab50e278ff497ee4d1fe69b29ca0a9a47cd954bb17963628fa417933c6eb1"}, - {file = "ruff-0.1.13-py3-none-musllinux_1_2_i686.whl", hash = "sha256:f57de973de4edef3ad3044d6a50c02ad9fc2dff0d88587f25f1a48e3f72edf5e"}, - {file = "ruff-0.1.13-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:7a36fa90eb12208272a858475ec43ac811ac37e91ef868759770b71bdabe27b6"}, - {file = "ruff-0.1.13-py3-none-win32.whl", hash = "sha256:a623349a505ff768dad6bd57087e2461be8db58305ebd5577bd0e98631f9ae69"}, - {file = "ruff-0.1.13-py3-none-win_amd64.whl", hash = "sha256:f988746e3c3982bea7f824c8fa318ce7f538c4dfefec99cd09c8770bd33e6539"}, - {file = "ruff-0.1.13-py3-none-win_arm64.whl", hash = "sha256:6bbbc3042075871ec17f28864808540a26f0f79a4478c357d3e3d2284e832998"}, - {file = "ruff-0.1.13.tar.gz", hash = "sha256:e261f1baed6291f434ffb1d5c6bd8051d1c2a26958072d38dfbec39b3dda7352"}, + {file = "ruff-0.9.1-py3-none-linux_armv6l.whl", hash = "sha256:84330dda7abcc270e6055551aca93fdde1b0685fc4fd358f26410f9349cf1743"}, + {file = "ruff-0.9.1-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:3cae39ba5d137054b0e5b472aee3b78a7c884e61591b100aeb544bcd1fc38d4f"}, + {file = "ruff-0.9.1-py3-none-macosx_11_0_arm64.whl", hash = "sha256:50c647ff96f4ba288db0ad87048257753733763b409b2faf2ea78b45c8bb7fcb"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f0c8b149e9c7353cace7d698e1656ffcf1e36e50f8ea3b5d5f7f87ff9986a7ca"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:beb3298604540c884d8b282fe7625651378e1986c25df51dec5b2f60cafc31ce"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:39d0174ccc45c439093971cc06ed3ac4dc545f5e8bdacf9f067adf879544d969"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:69572926c0f0c9912288915214ca9b2809525ea263603370b9e00bed2ba56dbd"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:937267afce0c9170d6d29f01fcd1f4378172dec6760a9f4dface48cdabf9610a"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:186c2313de946f2c22bdf5954b8dd083e124bcfb685732cfb0beae0c47233d9b"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3f94942a3bb767675d9a051867c036655fe9f6c8a491539156a6f7e6b5f31831"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:728d791b769cc28c05f12c280f99e8896932e9833fef1dd8756a6af2261fd1ab"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:2f312c86fb40c5c02b44a29a750ee3b21002bd813b5233facdaf63a51d9a85e1"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_i686.whl", hash = "sha256:ae017c3a29bee341ba584f3823f805abbe5fe9cd97f87ed07ecbf533c4c88366"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:5dc40a378a0e21b4cfe2b8a0f1812a6572fc7b230ef12cd9fac9161aa91d807f"}, + {file = "ruff-0.9.1-py3-none-win32.whl", hash = "sha256:46ebf5cc106cf7e7378ca3c28ce4293b61b449cd121b98699be727d40b79ba72"}, + {file = "ruff-0.9.1-py3-none-win_amd64.whl", hash = "sha256:342a824b46ddbcdddd3abfbb332fa7fcaac5488bf18073e841236aadf4ad5c19"}, + {file = "ruff-0.9.1-py3-none-win_arm64.whl", hash = "sha256:1cd76c7f9c679e6e8f2af8f778367dca82b95009bc7b1a85a47f1521ae524fa7"}, + {file = "ruff-0.9.1.tar.gz", hash = "sha256:fd2b25ecaf907d6458fa842675382c8597b3c746a2dde6717fe3415425df0c17"}, ] [[package]] diff --git a/pyproject.toml b/pyproject.toml index 0a562cbb..27ac74b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,6 +116,8 @@ exclude_also = [ [tool.ruff] src = ["src", "tests"] + +[tool.ruff.lint] select = [ "E", # pycodestyle "W", # pycodestyle @@ -144,7 +146,6 @@ ignore = [ "S101", # assert "S307", # hands off my eval "SIM300", # Yoda rocks in asserts - "PGH001", # No eval lol? "PGH003", # leave my type: ignores alone "B006", # mutable argument defaults "DTZ001", # datetimes in tests diff --git a/src/cattr/__init__.py b/src/cattr/__init__.py index 6c262fe8..50f2a063 100644 --- a/src/cattr/__init__.py +++ b/src/cattr/__init__.py @@ -2,16 +2,16 @@ from .gen import override __all__ = ( - "global_converter", - "unstructure", - "structure", - "structure_attrs_fromtuple", - "structure_attrs_fromdict", - "UnstructureStrategy", "BaseConverter", "Converter", "GenConverter", + "UnstructureStrategy", + "global_converter", "override", + "structure", + "structure_attrs_fromdict", + "structure_attrs_fromtuple", + "unstructure", ) from cattrs import global_converter diff --git a/src/cattr/preconf/json.py b/src/cattr/preconf/json.py index d590bd6d..ac773984 100644 --- a/src/cattr/preconf/json.py +++ b/src/cattr/preconf/json.py @@ -2,4 +2,4 @@ from cattrs.preconf.json import JsonConverter, configure_converter, make_converter -__all__ = ["configure_converter", "JsonConverter", "make_converter"] +__all__ = ["JsonConverter", "configure_converter", "make_converter"] diff --git a/src/cattr/preconf/msgpack.py b/src/cattr/preconf/msgpack.py index 1a579d63..bb90250c 100644 --- a/src/cattr/preconf/msgpack.py +++ b/src/cattr/preconf/msgpack.py @@ -2,4 +2,4 @@ from cattrs.preconf.msgpack import MsgpackConverter, configure_converter, make_converter -__all__ = ["configure_converter", "make_converter", "MsgpackConverter"] +__all__ = ["MsgpackConverter", "configure_converter", "make_converter"] diff --git a/src/cattr/preconf/orjson.py b/src/cattr/preconf/orjson.py index 44509901..569ec181 100644 --- a/src/cattr/preconf/orjson.py +++ b/src/cattr/preconf/orjson.py @@ -2,4 +2,4 @@ from cattrs.preconf.orjson import OrjsonConverter, configure_converter, make_converter -__all__ = ["configure_converter", "make_converter", "OrjsonConverter"] +__all__ = ["OrjsonConverter", "configure_converter", "make_converter"] diff --git a/src/cattr/preconf/pyyaml.py b/src/cattr/preconf/pyyaml.py index 63d39f18..6bf8b369 100644 --- a/src/cattr/preconf/pyyaml.py +++ b/src/cattr/preconf/pyyaml.py @@ -2,4 +2,4 @@ from cattrs.preconf.pyyaml import PyyamlConverter, configure_converter, make_converter -__all__ = ["configure_converter", "make_converter", "PyyamlConverter"] +__all__ = ["PyyamlConverter", "configure_converter", "make_converter"] diff --git a/src/cattr/preconf/tomlkit.py b/src/cattr/preconf/tomlkit.py index 6add7319..7c0e7039 100644 --- a/src/cattr/preconf/tomlkit.py +++ b/src/cattr/preconf/tomlkit.py @@ -2,4 +2,4 @@ from cattrs.preconf.tomlkit import TomlkitConverter, configure_converter, make_converter -__all__ = ["configure_converter", "make_converter", "TomlkitConverter"] +__all__ = ["TomlkitConverter", "configure_converter", "make_converter"] diff --git a/src/cattr/preconf/ujson.py b/src/cattr/preconf/ujson.py index ef85c475..2efbbeeb 100644 --- a/src/cattr/preconf/ujson.py +++ b/src/cattr/preconf/ujson.py @@ -2,4 +2,4 @@ from cattrs.preconf.ujson import UjsonConverter, configure_converter, make_converter -__all__ = ["configure_converter", "make_converter", "UjsonConverter"] +__all__ = ["UjsonConverter", "configure_converter", "make_converter"] diff --git a/src/cattrs/__init__.py b/src/cattrs/__init__.py index 18ab4aea..2252272c 100644 --- a/src/cattrs/__init__.py +++ b/src/cattrs/__init__.py @@ -22,24 +22,24 @@ "Converter", "ForbiddenExtraKeysError", "GenConverter", + "IterableValidationError", + "IterableValidationNote", + "SimpleStructureHook", + "StructureHandlerNotFoundError", + "UnstructureStrategy", "get_structure_hook", "get_unstructure_hook", "global_converter", - "IterableValidationError", - "IterableValidationNote", "override", - "register_structure_hook_func", "register_structure_hook", - "register_unstructure_hook_func", + "register_structure_hook_func", "register_unstructure_hook", - "SimpleStructureHook", + "register_unstructure_hook_func", + "structure", "structure_attrs_fromdict", "structure_attrs_fromtuple", - "structure", - "StructureHandlerNotFoundError", "transform_error", "unstructure", - "UnstructureStrategy", ] #: The global converter. Prefer creating your own if customizations are required. diff --git a/src/cattrs/_compat.py b/src/cattrs/_compat.py index 0603bd3c..d8f74482 100644 --- a/src/cattrs/_compat.py +++ b/src/cattrs/_compat.py @@ -48,13 +48,13 @@ __all__ = [ "ANIES", - "adapted_fields", - "fields_dict", "ExceptionGroup", "ExtensionsTypedDict", + "TypeAlias", + "adapted_fields", + "fields_dict", "has", "is_typeddict", - "TypeAlias", ] try: @@ -281,10 +281,8 @@ def get_newtype_base(typ: Any) -> Optional[type]: from typing_extensions import NotRequired, Required def is_union_type(obj): - return ( - obj is Union - or isinstance(obj, _UnionGenericAlias) - and obj.__origin__ is Union + return obj is Union or ( + isinstance(obj, _UnionGenericAlias) and obj.__origin__ is Union ) def get_newtype_base(typ: Any) -> Optional[type]: @@ -330,10 +328,8 @@ def is_sequence(type: Any) -> bool: or ( type.__class__ is _GenericAlias and ( - (origin is not tuple) - and is_subclass(origin, TypingSequence) - or origin is tuple - and type.__args__[1] is ... + ((origin is not tuple) and is_subclass(origin, TypingSequence)) + or (origin is tuple and type.__args__[1] is ...) ) ) or (origin in (list, deque, AbcMutableSequence, AbcSequence)) diff --git a/src/cattrs/cols.py b/src/cattrs/cols.py index fc2ac986..701bb53b 100644 --- a/src/cattrs/cols.py +++ b/src/cattrs/cols.py @@ -46,22 +46,22 @@ from .converters import BaseConverter __all__ = [ + "defaultdict_structure_factory", "is_any_set", "is_defaultdict", "is_frozenset", - "is_namedtuple", "is_mapping", - "is_set", + "is_namedtuple", "is_sequence", - "defaultdict_structure_factory", + "is_set", "iterable_unstructure_factory", "list_structure_factory", - "namedtuple_structure_factory", - "namedtuple_unstructure_factory", - "namedtuple_dict_structure_factory", - "namedtuple_dict_unstructure_factory", "mapping_structure_factory", "mapping_unstructure_factory", + "namedtuple_dict_structure_factory", + "namedtuple_dict_unstructure_factory", + "namedtuple_structure_factory", + "namedtuple_unstructure_factory", ] diff --git a/src/cattrs/converters.py b/src/cattrs/converters.py index 80685fdb..4f65bb47 100644 --- a/src/cattrs/converters.py +++ b/src/cattrs/converters.py @@ -97,7 +97,7 @@ ) from .types import SimpleStructureHook -__all__ = ["UnstructureStrategy", "BaseConverter", "Converter", "GenConverter"] +__all__ = ["BaseConverter", "Converter", "GenConverter", "UnstructureStrategy"] T = TypeVar("T") V = TypeVar("V") @@ -165,16 +165,16 @@ class BaseConverter: """Converts between structured and unstructured data.""" __slots__ = ( - "_unstructure_func", - "_unstructure_attrs", - "_structure_attrs", "_dict_factory", - "_union_struct_registry", - "_structure_func", "_prefer_attrib_converters", - "detailed_validation", "_struct_copy_skip", + "_structure_attrs", + "_structure_func", + "_union_struct_registry", "_unstruct_copy_skip", + "_unstructure_attrs", + "_unstructure_func", + "detailed_validation", ) def __init__( @@ -1020,10 +1020,10 @@ class Converter(BaseConverter): """A converter which generates specialized un/structuring functions.""" __slots__ = ( - "omit_if_default", + "_unstruct_collection_overrides", "forbid_extra_keys", + "omit_if_default", "type_overrides", - "_unstruct_collection_overrides", ) def __init__( diff --git a/src/cattrs/disambiguators.py b/src/cattrs/disambiguators.py index 80288024..6fc5d9da 100644 --- a/src/cattrs/disambiguators.py +++ b/src/cattrs/disambiguators.py @@ -26,7 +26,7 @@ if TYPE_CHECKING: from .converters import BaseConverter -__all__ = ["is_supported_union", "create_default_dis_func"] +__all__ = ["create_default_dis_func", "is_supported_union"] def is_supported_union(typ: Any) -> bool: diff --git a/src/cattrs/gen/__init__.py b/src/cattrs/gen/__init__.py index 946eac18..7a562c47 100644 --- a/src/cattrs/gen/__init__.py +++ b/src/cattrs/gen/__init__.py @@ -39,14 +39,14 @@ from ..converters import BaseConverter __all__ = [ - "make_dict_unstructure_fn", "make_dict_structure_fn", - "make_iterable_unstructure_fn", + "make_dict_structure_fn_from_attrs", + "make_dict_unstructure_fn", + "make_dict_unstructure_fn_from_attrs", "make_hetero_tuple_unstructure_fn", - "make_mapping_unstructure_fn", + "make_iterable_unstructure_fn", "make_mapping_structure_fn", - "make_dict_unstructure_fn_from_attrs", - "make_dict_structure_fn_from_attrs", + "make_mapping_unstructure_fn", ] @@ -865,7 +865,7 @@ def mapping_unstructure_factory( lines = [f"def {fn_name}(mapping):"] - if unstructure_to is dict or unstructure_to is None and origin is dict: + if unstructure_to is dict or (unstructure_to is None and origin is dict): if kh is None and val_handler is None: # Simplest path. return dict diff --git a/src/cattrs/gen/typeddicts.py b/src/cattrs/gen/typeddicts.py index 827f5086..dcb58641 100644 --- a/src/cattrs/gen/typeddicts.py +++ b/src/cattrs/gen/typeddicts.py @@ -45,7 +45,7 @@ def get_annots(cl) -> dict[str, Any]: if TYPE_CHECKING: from ..converters import BaseConverter -__all__ = ["make_dict_unstructure_fn", "make_dict_structure_fn"] +__all__ = ["make_dict_structure_fn", "make_dict_unstructure_fn"] T = TypeVar("T", bound=TypedDict) diff --git a/src/cattrs/preconf/json.py b/src/cattrs/preconf/json.py index 08851a75..199c574d 100644 --- a/src/cattrs/preconf/json.py +++ b/src/cattrs/preconf/json.py @@ -13,6 +13,8 @@ from ..strategies import configure_union_passthrough from . import is_primitive_enum, literals_with_enums_unstructure_factory, wrap +__all__ = ["JsonConverter", "configure_converter", "make_converter"] + T = TypeVar("T") @@ -24,7 +26,7 @@ def loads(self, data: Union[bytes, str], cl: type[T], **kwargs: Any) -> T: return self.structure(loads(data, **kwargs), cl) -def configure_converter(converter: BaseConverter): +def configure_converter(converter: BaseConverter) -> None: """ Configure the converter for use with the stdlib json module. diff --git a/src/cattrs/preconf/msgpack.py b/src/cattrs/preconf/msgpack.py index da01418e..92876418 100644 --- a/src/cattrs/preconf/msgpack.py +++ b/src/cattrs/preconf/msgpack.py @@ -12,6 +12,8 @@ from ..strategies import configure_union_passthrough from . import is_primitive_enum, literals_with_enums_unstructure_factory, wrap +__all__ = ["MsgpackConverter", "configure_converter", "make_converter"] + T = TypeVar("T") @@ -23,7 +25,7 @@ def loads(self, data: bytes, cl: type[T], **kwargs: Any) -> T: return self.structure(loads(data, **kwargs), cl) -def configure_converter(converter: BaseConverter): +def configure_converter(converter: BaseConverter) -> None: """ Configure the converter for use with the msgpack library. diff --git a/src/cattrs/preconf/orjson.py b/src/cattrs/preconf/orjson.py index 112534eb..0726ef04 100644 --- a/src/cattrs/preconf/orjson.py +++ b/src/cattrs/preconf/orjson.py @@ -17,6 +17,8 @@ from ..strategies import configure_union_passthrough from . import is_primitive_enum, literals_with_enums_unstructure_factory, wrap +__all__ = ["OrjsonConverter", "configure_converter", "make_converter"] + T = TypeVar("T") @@ -28,7 +30,7 @@ def loads(self, data: Union[bytes, bytearray, memoryview, str], cl: type[T]) -> return self.structure(loads(data), cl) -def configure_converter(converter: Converter): +def configure_converter(converter: Converter) -> None: """ Configure the converter for use with the orjson library. diff --git a/src/cattrs/preconf/pyyaml.py b/src/cattrs/preconf/pyyaml.py index a6a4bfa9..b1b88540 100644 --- a/src/cattrs/preconf/pyyaml.py +++ b/src/cattrs/preconf/pyyaml.py @@ -12,6 +12,8 @@ from ..strategies import configure_union_passthrough from . import validate_datetime, wrap +__all__ = ["PyyamlConverter", "configure_converter", "make_converter"] + T = TypeVar("T") @@ -29,7 +31,7 @@ def loads(self, data: str, cl: type[T]) -> T: return self.structure(safe_load(data), cl) -def configure_converter(converter: BaseConverter): +def configure_converter(converter: BaseConverter) -> None: """ Configure the converter for use with the pyyaml library. diff --git a/src/cattrs/preconf/tomlkit.py b/src/cattrs/preconf/tomlkit.py index ace6c360..802df9b6 100644 --- a/src/cattrs/preconf/tomlkit.py +++ b/src/cattrs/preconf/tomlkit.py @@ -15,6 +15,8 @@ from ..strategies import configure_union_passthrough from . import validate_datetime, wrap +__all__ = ["TomlkitConverter", "configure_converter", "make_converter"] + T = TypeVar("T") _enum_value_getter = attrgetter("_value_") diff --git a/src/cattrs/preconf/ujson.py b/src/cattrs/preconf/ujson.py index afb79b98..8f330615 100644 --- a/src/cattrs/preconf/ujson.py +++ b/src/cattrs/preconf/ujson.py @@ -13,6 +13,8 @@ from ..strategies import configure_union_passthrough from . import is_primitive_enum, literals_with_enums_unstructure_factory, wrap +__all__ = ["UjsonConverter", "configure_converter", "make_converter"] + T = TypeVar("T") diff --git a/src/cattrs/strategies/_unions.py b/src/cattrs/strategies/_unions.py index c8872019..816a2620 100644 --- a/src/cattrs/strategies/_unions.py +++ b/src/cattrs/strategies/_unions.py @@ -7,9 +7,9 @@ from cattrs._compat import get_newtype_base, is_literal, is_subclass, is_union_type __all__ = [ - "default_tag_generator", "configure_tagged_union", "configure_union_passthrough", + "default_tag_generator", ] diff --git a/src/cattrs/typealiases.py b/src/cattrs/typealiases.py index c153f12e..d3a20c48 100644 --- a/src/cattrs/typealiases.py +++ b/src/cattrs/typealiases.py @@ -13,7 +13,7 @@ if TYPE_CHECKING: from .converters import BaseConverter -__all__ = ["is_type_alias", "get_type_alias_base", "type_alias_structure_factory"] +__all__ = ["get_type_alias_base", "is_type_alias", "type_alias_structure_factory"] if sys.version_info >= (3, 12): from types import GenericAlias diff --git a/tests/test_converter.py b/tests/test_converter.py index dff77f36..b071ae28 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -627,7 +627,7 @@ class C: ) expected = seq_type(C(a=cl(*vals), b=cl(*vals)) for _ in range(5)) - assert type(outputs) == seq_type + assert type(outputs) is seq_type assert outputs == expected diff --git a/tests/test_generics_696.py b/tests/test_generics_696.py index c56c894f..9cb0ddfa 100644 --- a/tests/test_generics_696.py +++ b/tests/test_generics_696.py @@ -30,7 +30,7 @@ class C(Generic[T]): c_mapping = generate_mapping(C[str]) atype = fields(C[str]).a.type - assert c_mapping[atype.__name__] == str + assert c_mapping[atype.__name__] is str assert genconverter.structure({"a": "1"}, C[str]) == C("1") @@ -40,10 +40,10 @@ class D(Generic[TD]): d_mapping = generate_mapping(D) atype = fields(D).a.type - assert d_mapping[atype.__name__] == str + assert d_mapping[atype.__name__] is str # Defaults to string - assert d_mapping[atype.__name__] == str + assert d_mapping[atype.__name__] is str assert genconverter.structure({"a": "1"}, D) == D("1") # But allows other types diff --git a/tests/test_typeddicts.py b/tests/test_typeddicts.py index 3456cd67..35085444 100644 --- a/tests/test_typeddicts.py +++ b/tests/test_typeddicts.py @@ -69,7 +69,7 @@ def get_annot(t) -> dict: NotRequired[param_to_args[nrb]] if nrb in param_to_args else v ) else: - res[k] = param_to_args[v] if v in param_to_args else v + res[k] = param_to_args.get(v, v) return res # Origin is `None`, so this is a subclass for a generic typeddict. @@ -81,7 +81,7 @@ def get_annot(t) -> dict: NotRequired[mapping[nrb.__name__]] if nrb.__name__ in mapping else v ) else: - res[k] = mapping[v.__name__] if v.__name__ in mapping else v + res[k] = mapping.get(v.__name__, v) return res return get_annots(t) diff --git a/tests/test_unstructure.py b/tests/test_unstructure.py index 40830ec8..3f9b52da 100644 --- a/tests/test_unstructure.py +++ b/tests/test_unstructure.py @@ -130,5 +130,5 @@ def test_seq_of_simple_classes_unstructure(cls_and_vals, seq_type: type): inputs = seq_type(cl(*vals, **kwargs) for cl, vals, kwargs in cls_and_vals) outputs = converter.unstructure(inputs) - assert type(outputs) == seq_type - assert all(type(e) is dict for e in outputs) # noqa: E721 + assert type(outputs) is seq_type + assert all(type(e) is dict for e in outputs) diff --git a/tests/test_validation.py b/tests/test_validation.py index 73df70e5..e8bed5a7 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -57,7 +57,7 @@ class Test: with pytest.raises(ClassValidationError) as exc: c.structure({"a": 1, "b": "c", "c": "1"}, Test) - assert type(exc.value.exceptions[0]) == ValueError + assert type(exc.value.exceptions[0]) is ValueError assert str(exc.value.exceptions[0].args[0]) == "'b' must be in ['a', 'b'] (got 'c')" diff --git a/tests/typeddicts.py b/tests/typeddicts.py index ff4c93d5..ba488010 100644 --- a/tests/typeddicts.py +++ b/tests/typeddicts.py @@ -32,10 +32,7 @@ def gen_typeddict_attr_names(): """Typed dicts can have periods in their field names.""" - counter = 0 - for n in gen_attr_names(): - counter += 1 - + for counter, n in enumerate(gen_attr_names()): if counter % 2 == 0: n = f"{n}.suffix"