Skip to content
New issue

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

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

Already on GitHub? # to your account

Bug: class Data(Protocol) triggers pyright type error #276

Closed
Andrei-Pozolotin opened this issue Feb 3, 2025 · 1 comment · Fixed by #278
Closed

Bug: class Data(Protocol) triggers pyright type error #276

Andrei-Pozolotin opened this issue Feb 3, 2025 · 1 comment · Fixed by #278
Labels
bug Something isn't working

Comments

@Andrei-Pozolotin
Copy link

The newly introduced in 1.9.1 data protocol:
https://github.com/konradhalas/dacite/blob/master/dacite/data.py

class Data(Protocol):
    def keys(self): ...
    def __getitem__(self, item): ...
    def __contains__(self, item): ...

is type-check incompatible with built-in dict type hint:

class dict(MutableMapping[_KT, _VT]):
    def keys(self) -> dict_keys[_KT, _VT]: ...
    def __getitem__(self, key: _KT, /) -> _VT: ...
    def __contains__(self, key: _KT, /) -> bool: ...  

and triggers the following error with pyright

    "dict[str, Any]" is incompatible with protocol "Data"
      "keys" is an incompatible type
        Type "() -> dict_keys[str, Any]" is not assignable to type "() -> None"
          Function return type "dict_keys[str, Any]" is incompatible with type "None"
            "dict_keys[str, Any]" is not assignable to "None"
      "__getitem__" is an incompatible type
        Type "(key: str, /) -> Any" is not assignable to type "(item: Unknown) -> None"
          Missing keyword parameter "item"
    ... (reportArgumentType)

The following change resolves the issue:

class Data(Protocol):
    def keys(self) -> Any: ...
    def __getitem__(self, item,/) -> Any: ...
    def __contains__(self, item,/) -> bool: ...
@Andrei-Pozolotin
Copy link
Author

works in 1.9.2, thank you.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant