help wanted
Repository metrics
- Stars
- (1,082 個のスター)
- PR merge metrics
- (30d に merged PR はありません)
説明
The trick explained here used to make typegaurd use the runtime types but MyPy use the static-type-checking types sadly does not work with Pyright.
from typing import TYPE_CHECKING, Any, TypeAlias, reveal_type
_STATIC_TYPE_CHECKING = TYPE_CHECKING # workaround for typeguard
if _STATIC_TYPE_CHECKING:
Foo: TypeAlias = Any
else:
class MetaFoo(type):
def __instancecheck__(cls, obj):
...
class Foo(metaclass=MetaFoo): # type: ignore[no-redef]
pass
if TYPE_CHECKING:
Bar: TypeAlias = Any
else:
class MetaBar(type):
def __instancecheck__(cls, obj):
...
class Bar(metaclass=MetaBar): # type: ignore[no-redef]
pass
reveal_type(Foo) # pyright output: Type of "Foo" is "Type[Foo]"
reveal_type(Bar) # pyright output: Type of "Bar" is "Any"
So this gives an error:
import inflect
p = inflect.engine()
p.plural("apple") # Argument of type "Literal['apple']" cannot be assigned to parameter "text" of type "Word" in function "plural"
# "Literal['apple']" is incompatible with "Word" [reportArgumentType]