jaraco/inflect

Pyright incompatibility

Open

#210 opened on Apr 4, 2024

 (3 comments) (3 reactions) (0 assignees)Python (125 forks)github user discovery
help wanted

Repository metrics

Stars
 (1,082 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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]

Contributor guide