8000 `issubclass()` doesn't narrow down types of type variables · Issue #7920 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
issubclass() doesn't narrow down types of type variables #7920
Closed
@ambv

Description

@ambv

Example:

from typing import *

TypeT = TypeVar("TypeT", bound=type)

class Base:
    field: str = "Hey"

class C1:
    def method(self, other: type) -> str:
        if issubclass(other, Base):
            # reveal_type(other) == Type[Base]
            return other.field
        return "Hi"

class C2(Generic[TypeT]):
    def method(self, other: TypeT) -> str:
        if issubclass(other, Base):
            # reveal_type(other) == TypeT`1
            return other.field  # mypy error: "TypeT" has no attribute "field"
        return "Hi"

Actual behavior as of 0.740 included in the comments above. Expected behavior would be to not raise an error on the penultimate line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0