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

Integrate mypy into development process (part of #116) #120

Merged
merged 13 commits into from
Apr 30, 2024

Conversation

binaryDiv
Copy link
Contributor

@binaryDiv binaryDiv commented Apr 30, 2024

This PR integrates mypy as a static type checker into the development environment and the CI pipeline. To do that, it fixes a bunch of typing issues in the library code. It solves a part of #116 (but not all of it as that would be too big of a scope).

List of general changes:

  • Add mypy to dev environment (tox, Makefile)
  • Add mypy to CI pipeline (GitHub Actions)
  • Set mypy rules to strict (apart from no_implicit_reexport)

List of code changes:

  • Add a lot of missing type hints and fix some incorrect/inaccurate ones
  • Replace some type checks with more type-safe ones (e.g. isinstance instead of type(...) is)
  • Exception classes: use relative imports to import base ValidationError
  • Improve dynamic keyword argument construction in some classes like here
  • FloatToDecimalValidator: min_value/max_value will not be cast to string if specified as integer anymore (see commit)
  • EnumValidator: Improve type checking of Enum class when creating the validator (9cbf9f9)
  • DataclassValidator: (0b6ed6e)
    • Improve check if given dataclass is actually a dataclass
    • Simplified dataclass check and error handling: There is now only one error message for "parameter is a class that is not a dataclass" and "parameter is an instance of a dataclass".
    • Properly type dataclasses.Field with type parameter (requires extra code because in Python 3.8 it is not a Generic yet)
  • Use parametrized Pattern type hints for regex patterns (uses typing.Pattern for now, because re.Pattern is not yet parametrized in Python 3.8) (a0d0f54)
  • ValidataclassMixin: Add a # type: ignore[call-overload] for the warning about asdict (28110ce)
  • Add # type: ignore[override] for validate() overrides in validator classes that return different types than their base validators (e.g. DecimalValidator returning Decimal, which is not compatible with StringValidator returning str) (712ac98)
  • Add # type: ignore for sentinel object creation (3965efe)
    • There might be a better way to do this, but sentinels are weird, there should be language support for it.
  • unset_to_none(): Use isinstance(value, UnsetValueType) instead of is UnsetValue (again, sentinels are weird) (5ceea74)
  • Fix typing in DateTimeRange and DateTimeOffsetRange (f7dd402)
  • Various small changes to fix mypy issues

@binaryDiv binaryDiv added testing Related to testing (e.g. unit tests) refactoring Code refactoring, clean up and other code maintenance work. labels Apr 30, 2024
@binaryDiv binaryDiv self-assigned this Apr 30, 2024
@binaryDiv binaryDiv merged commit d0f0d3a into main Apr 30, 2024
7 checks passed
@binaryDiv binaryDiv deleted the 116-mypy-first-steps branch April 30, 2024 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Code refactoring, clean up and other code maintenance work. testing Related to testing (e.g. unit tests)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants