Zuehlke / ConfZ

ConfZ is a configuration management library for Python based on pydantic.
https://confz.readthedocs.io
MIT License
226 stars 10 forks source link

Mypy false positive: metaclass conflict #96

Closed ofacklam closed 3 months ago

ofacklam commented 3 months ago

Mypy detects a metaclass conflict as soon as we subclass the ConfZ class as per default use case:

reproduce.py:

from confz import ConfZ, ConfZEnvSource

class MyConfig(ConfZ):
    my_config_variable: bool

    CONFIG_SOURCES = [ConfZEnvSource(allow_all=True)]

Output of mypy:

❯ mypy .
reproduce.py:4: error: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases  [misc]
Found 1 error in 1 file (checked 1 source file)

Used configuration:

❯ python3 --version
Python 3.10.12
❯ pip3 list
Package           Version
----------------- -------
confz             1.7.0
mypy              1.10.0
mypy-extensions   1.0.0
pip               22.0.2
pydantic          1.10.15
python-dotenv     0.21.1
PyYAML            6.0.1
setuptools        59.6.0
toml              0.10.2
tomli             2.0.1
typing_extensions 4.12.1

Note: It seems to still be the case with confZ 2 when using BaseConfig

silvanmelchior commented 3 months ago

Indeed, seams to be a limitation of MyPy, it doesn't correctly recognize the metaclass because pydantic's Metaclass does not inherit from type (https://mypy.readthedocs.io/en/stable/metaclasses.html, last point).

Thanks for pointing it out, I will adjust the docs.

Interestingly, this problem did not exist with older versions of MyPy (tried it with 0.940, there it still accepts it).

silvanmelchior commented 3 months ago

Done, see https://confz.readthedocs.io/en/latest/usage/mypy.html