Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyright incompatibility #210

Open
Jeremiah-England opened this issue Apr 4, 2024 · 3 comments
Open

Pyright incompatibility #210

Jeremiah-England opened this issue Apr 4, 2024 · 3 comments

Comments

@Jeremiah-England
Copy link

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
Copy link
Owner

jaraco commented Apr 23, 2024

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
Copy link

@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
Copy link
Owner

jaraco commented Apr 23, 2024

I welcome others to tackle this architectural/design concern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants