jaraco / inflect

Correctly generate plurals, ordinals, indefinite articles; convert numbers to words
https://pypi.org/project/inflect
MIT License
949 stars 104 forks source link

Pyright incompatibility #210

Open Jeremiah-England opened 4 months ago

Jeremiah-England commented 4 months ago

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]
jaraco commented 4 months ago

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?

GhostLyrics commented 4 months ago

@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.

jaraco commented 4 months ago

I welcome others to tackle this architectural/design concern.