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

Pyright not recognizing metaclass keyword arguments #2436

Closed
StockerMC opened this issue Oct 15, 2021 · 4 comments
Closed

Pyright not recognizing metaclass keyword arguments #2436

StockerMC opened this issue Oct 15, 2021 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@StockerMC
Copy link

StockerMC commented Oct 15, 2021

Describe the bug
Pyright doesn't recognize the keyword arguments of a metaclass when passing them in the class definition, though it does for classes with __init_subclass__ defined.

Screenshots or Code

from typing import TypeVar, Tuple, Dict, Any, Type, TYPE_CHECKING

CMT = TypeVar('CMT', bound='CommandMeta')

class BaseCommand:
    if TYPE_CHECKING:
        __foo__: int
        __bar__: str

    def __init_subclass__(cls, *, foo: int, bar: str) -> None: ...

class CommandMeta(type):
    if TYPE_CHECKING:
        __foo__: int
        __bar__: str

    def __new__(
        cls: Type[CMT],
        cls_name: str,
        bases: Tuple[type, ...],
        attrs: Dict[str, Any],
        *,
        foo: int,
        bar: str,
    ) -> CMT: ...


class One(BaseCommand, foo=1): ...  # Argument missing for parameter "bar"
class Two(metaclass=CommandMeta, foo=''): ...  # No error for the incorrect type or the missing parameter

VS Code extension or command-line
Are you running pyright as a VS Code extension or a command-line tool?
Command line (version 1.1.178)

@erictraut
Copy link
Collaborator

Yes, you should use __init_subclass__ if you want type checking for metaclass parameters. The behavior is consistent with mypy.

@erictraut erictraut added as designed Not a bug, working as intended enhancement request New feature or request labels Oct 15, 2021
@erictraut
Copy link
Collaborator

Looks like this is a known missing feature in mypy. This issue was filed almost five years ago, and it still hasn't been addressed.

I think that metaclasses that take typed keyword parameters are pretty rare, so it's understandable that such a feature would be low priority for a type checker.

For now I'm going to close this request, but we can will reconsider in the future if there is sufficient interest (additional requests or upvotes on this issue).

@Zomatree
Copy link

I think this should be added, python uses them in the stdlib1 and i commonly use them in my code as they can remove boilerplate.

I dont think we should be pinning not adding features due to mypy not supporting them, mypy is notorious for being slow at adding new features and fixing bugs.

@erictraut erictraut reopened this Dec 25, 2021
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Dec 26, 2021
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.201, which I just published. It will also be included in the next release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants