Skip to content

mypy: Metaclass conflict in v2 #89

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

Closed
ja-albert opened this issue Aug 4, 2023 · 2 comments
Closed

mypy: Metaclass conflict in v2 #89

ja-albert opened this issue Aug 4, 2023 · 2 comments
Labels
typing Related to typing v2 Version 2 related

Comments

@ja-albert
Copy link

Hi :) Again, thank you for your work and your quick fix in my previous issue!

I found a new problem in v2.0.0b1, which I could not reproduce in v1.0.0. MWE:

from pydantic_xml import BaseXmlModel

class A(BaseXmlModel): pass

Running mypy on it results in:

$ mypy test.py
test.py:3: error: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases  [misc]

I'm unsure if this is a mypy bug or not, but in my normal code, this results in this mypy error for each subclass of BaseXmlModel I have.

I also found this mypy issue which suggests that adding a # type: ignore[misc] to the class BaseXmlModel line could be a workaround: python/mypy#14033 (comment)
But to be honest, I do not know enough about Python metaclasses to give you a better problem analysis and solution proposal.
On the other hand, you may already have seen a similar issue, as you apparently added the same type ignore a few lines above the BaseXmlModel definition for version 2: https://github.com/dapper91/pydantic-xml/blob/v2.0.0b1/pydantic_xml/model.py#L202

@dapper91
Copy link
Owner

dapper91 commented Aug 4, 2023

Hi @ja-albert.

Thanks for your report.

Yes, the problem is here.
mypy can't figure out that type(BaseModel) returns ModelMetaclass and thinks that BaseModel and BaseXmlModel have different metaclass roots which is prohibited in python.

The other problem is that ModelMetaclass located in an internal pydantic module which means it is not a public api and can be changed in the future.

Found the issue addressing that problem. They advice to import ModelMetaclass directly from pydantic._internal._model_construction.

I will fix it in the next release.

@dapper91 dapper91 added enhancement New feature or request v2 Version 2 related typing Related to typing and removed enhancement New feature or request labels Aug 8, 2023
@ja-albert
Copy link
Author

Thank you, the problem is fixed in the new release 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing Related to typing v2 Version 2 related
Projects
None yet
Development

No branches or pull requests

2 participants