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

__init_subclass__ not recognized if inheriting from class from different module #10021

Closed
giladreti opened this issue Feb 4, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@giladreti
Copy link

giladreti commented Feb 4, 2021

Bug Report

Mypy mistakenly does not recognize __init_subclass__ of parent class if defined in a different module.

child.py:4: error: Unexpected keyword argument "flag" for "__init_subclass__" of "object"

To Reproduce

parent.py:

class B:
    @classmethod
    def __init_subclass__(cls, /, flag: bool = False, **kwargs):
        # until https://github.com/python/mypy/issues/4660 is fixed
        super().__init_subclass__(**kwargs)  # type: ignore[call-arg]

child.py:

from .parent import B


class A(B, flag=True):
    pass

Expected Behavior

Mypy should recognize that A inherits from B and thus understand that it overrides the default __init_subclass__.
It already does so if they are both located in the same module.

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: mypy child.py
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
plugins = pydantic.mypy

follow_imports = silent
warn_redundant_casts = True
warn_unused_ignores = True
disallow_any_generics = True
check_untyped_defs = True
no_implicit_reexport = True


[pydantic-mypy]
init_forbid_extra = True
init_typed = True
warn_required_dynamic_aliases = True
  • Python version used: Python 3.8.6
  • Operating system and version: Linux ubuntu 5.10.0 x86_64 x86_64 x86_64 GNU/Linux
@giladreti giladreti added the bug mypy got something wrong label Feb 4, 2021
@JelleZijlstra
Copy link
Member

What happens without 'follow_imports = silent`? (For reference: https://mypy.readthedocs.io/en/latest/running_mypy.html#follow-imports )

@giladreti
Copy link
Author

Closing, since the comments in #8726 solved the issue. I had to make a package containing both modules and run mypy from outside the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants