python / python/mypy

metaclass/__init_subclass__ keyword checking

Open
#11,057 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-metaclasses topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

A way for plugins to mark a metaclass as safely passing keyword arguments to __init_subclass__.

Pitch

The current situation

Currently, if a class is inherited from that both has a custom metaclass and __init_subclass__ defined then there is no type checking done for the keyword arguments in __init_subclass__. For example the following is an error:

class Base:
    def __init_subclass__(cls, keyword: int = 0): ...
    
class Derived(Base, keyword='fail'): ...

error: Argument "keyword" to "__init_subclass__" of "Base" has incompatible type "str"; expected "int"

But this is not:

class Meta(type): ...

class Base(metaclass=Meta):
    def __init_subclass__(cls, keyword: int = 0): ...
    
class Derived(Base, keyword='fail'): ...

The specifics around this are in #7723.

My issue

I have a base class that operates similar to dataclass object. I have a plugin that provides the semantics to mypy already. However, this class uses a metaclass and __init_subclass__ and I would like type checking on the __init_subclass__ keyword arguments. The metaclass __new__ does not do anything with the keyword arguments but pass them along, so I know it's safe to do the type checking. I would like for my plugin to be able to tell mypy that this metaclass is safe.

Unfortunately I can't just get rid of the metaclass as it provides a class level __await__.

My current solution is just to hack the TypeInfo._fullname for the metaclass to be builtins.type.

The solutions that come to mind would be either:

  • A boolean on TypeInfo that says that the metaclass is __init_subclass__ safe (plugins just need to flip this in one of the existing hooks)
  • A way for plugins to manipulate the set of safe metaclasses that Checker uses (hook to customize Checker prior to run?)

I'm happy to do the work if a solution can be found.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Checker logic that determines which metaclasses are safe, then inspect TypeInfo and the existing plugin hooks mentioned in the issue. Compare the proposed TypeInfo flag with a hook for customizing the safe-metaclass set. Done means a plugin can mark a metaclass as safely forwarding init_subclass keywords and the example receives type checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.