mirror of https://github.com/astral-sh/ruff
3.7 KiB
3.7 KiB
Identical type display names in diagnostics
ty prints the fully qualified name to disambiguate objects with the same name.
Nested class
test.py:
class A:
class B:
pass
class C:
class B:
pass
a: A.B = C.B() # error: [invalid-assignment] "Object of type `test.C.B` is not assignable to `test.A.B`"
Nested class in function
test.py:
class B:
pass
def f(b: B):
class B:
pass
# error: [invalid-assignment] "Object of type `test.<locals of function 'f'>.B` is not assignable to `test.B`"
b = B()
Class from different modules
import a
import b
df: a.DataFrame = b.DataFrame() # error: [invalid-assignment] "Object of type `b.DataFrame` is not assignable to `a.DataFrame`"
def _(dfs: list[b.DataFrame]):
# TODO should be"Object of type `list[b.DataFrame]` is not assignable to `list[a.DataFrame]`
# error: [invalid-assignment] "Object of type `list[DataFrame]` is not assignable to `list[DataFrame]`"
dataframes: list[a.DataFrame] = dfs
a.py:
class DataFrame:
pass
b.py:
class DataFrame:
pass
Enum from different modules
import status_a
import status_b
# error: [invalid-assignment] "Object of type `Literal[status_b.Status.ACTIVE]` is not assignable to `status_a.Status`"
s: status_a.Status = status_b.Status.ACTIVE
status_a.py:
from enum import Enum
class Status(Enum):
ACTIVE = 1
INACTIVE = 2
status_b.py:
from enum import Enum
class Status(Enum):
ACTIVE = "active"
INACTIVE = "inactive"
Nested enum
test.py:
from enum import Enum
class A:
class B(Enum):
ACTIVE = "active"
INACTIVE = "inactive"
class C:
class B(Enum):
ACTIVE = "active"
INACTIVE = "inactive"
# error: [invalid-assignment] "Object of type `Literal[test.C.B.ACTIVE]` is not assignable to `test.A.B`"
a: A.B = C.B.ACTIVE
Class literals
import cls_a
import cls_b
# error: [invalid-assignment] "Object of type `<class 'cls_b.Config'>` is not assignable to `type[cls_a.Config]`"
config_class: type[cls_a.Config] = cls_b.Config
cls_a.py:
class Config:
pass
cls_b.py:
class Config:
pass
Generic aliases
import generic_a
import generic_b
# TODO should be error: [invalid-assignment] "Object of type `<class 'generic_b.Container[int]'>` is not assignable to `type[generic_a.Container[int]]`"
container: type[generic_a.Container[int]] = generic_b.Container[int]
generic_a.py:
from typing import Generic, TypeVar
T = TypeVar("T")
class Container(Generic[T]):
pass
generic_b.py:
from typing import Generic, TypeVar
T = TypeVar("T")
class Container(Generic[T]):
pass
Protocols
from typing import Protocol
import proto_a
import proto_b
# TODO should be error: [invalid-assignment] "Object of type `proto_b.Drawable` is not assignable to `proto_a.Drawable`"
def _(drawable_b: proto_b.Drawable):
drawable: proto_a.Drawable = drawable_b
proto_a.py:
from typing import Protocol
class Drawable(Protocol):
def draw(self) -> None: ...
proto_b.py:
from typing import Protocol
class Drawable(Protocol):
def draw(self) -> int: ...
TypedDict
from typing import TypedDict
import dict_a
import dict_b
def _(b_person: dict_b.Person):
# TODO should be error: [invalid-assignment] "Object of type `dict_b.Person` is not assignable to `dict_a.Person`"
person_var: dict_a.Person = b_person
dict_a.py:
from typing import TypedDict
class Person(TypedDict):
name: str
dict_b.py:
from typing import TypedDict
class Person(TypedDict):
name: bytes