inflect icon indicating copy to clipboard operation
inflect copied to clipboard

Pyright incompatibility

Open Jeremiah-England opened this issue 1 year ago • 3 comments

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]

Jeremiah-England avatar Apr 04 '24 22:04 Jeremiah-England

I'm sorry to hear that. I'm not sure there's much this project can do about the situation other than stop using Typeguard. This project switched from Pydantic to Typeguard in order to avoid expensive and compatibility-challenged dependencies. This project could, of course, fall back to classical imperative validation of parameters, which would be a substantial loss of sophistication in the code. Can I assume this report is a symptom of a larger issue to be addressed between Typeguard and Pyright?

jaraco avatar Apr 23 '24 07:04 jaraco

@jaraco Maybe it's worth contacting the Pyright maintainers? From what I've seen they were often quite quick regarding replies to issues and might have input on how to solve this most elegantly if you describe what you want to achieve.

GhostLyrics avatar Apr 23 '24 07:04 GhostLyrics

I welcome others to tackle this architectural/design concern.

jaraco avatar Apr 23 '24 13:04 jaraco