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
__slots__ requires explicit type definition and must be Any (and nothing else) when used with Protocols
#11013
Comments
__slots__ requires type definition when used with Protocols__slots__ requires explicit type definition when used with Protocols
|
Here's another oddity not fixed by typing # test_case3.py
from fractions import Fraction
from typing import Protocol, Tuple, TypeVar, runtime_checkable
_T_co = TypeVar("_T_co", covariant=True)
@runtime_checkable
class RationalInitializerT(Protocol[_T_co]):
__slots__: Tuple[str, ...] = ()
def __call__(self, numerator: int, denominator: int) -> _T_co:
...
f: RationalInitializerT = Fraction # <- errorsNow, remove # test_case4.py
from fractions import Fraction
from typing import Protocol, Tuple, TypeVar, runtime_checkable
_T_co = TypeVar("_T_co", covariant=True)
@runtime_checkable
class RationalInitializerT(Protocol[_T_co]):
# __slots__: Tuple[str, ...] = () # <- commented out
def __call__(self, numerator: int, denominator: int) -> _T_co:
...
f: RationalInitializerT = Fraction # <- works now?Another example: # test_case5.py
from typing import Callable, Iterable, Protocol, Tuple, Union, runtime_checkable
from mypy_extensions import VarArg
@runtime_checkable
class ProcessorT(Protocol):
__slots__: Tuple[str, ...] = ()
def __call__(
self,
*inputs: int,
) -> Union[int, Iterable[int]]:
...
def narrower_return_proc(*inputs: int) -> Iterable[int]:
return inputs
proc1: Callable[[VarArg(int)], Union[int, Iterable[int]]] = narrower_return_proc # <- works
proc2: ProcessorT = narrower_return_proc # <- errorsRemoving/commenting out |
|
BTW, adding @runtime_checkable
class SupportsInt(Protocol):
"""An ABC with one abstract method __int__."""
__slots__ = ()
@abstractmethod
def __int__(self) -> int:
passBut then I found this [source]: def runtime_checkable(cls: _TC) -> _TC: ...
@runtime_checkable
class SupportsInt(Protocol, metaclass=ABCMeta):
@abstractmethod
def __int__(self) -> int: ...I guess that's another way to "work around" trivial versions of the above problem, but it's not accessible (much less intuitive). I also have no idea how to make it work with forward references without reproducing entire swaths of code in # foo.py
from __future__ import annotations
from typing import Iterable, Protocol, Tuple, Union, runtime_checkable
class Foo:
... # lots of of members and methods here
@runtime_checkable
class FooProcessorT(Protocol):
__slots__: Tuple[str, ...] = ()
def __call__(
self,
*inputs: Foo,
) -> Union[Foo, Iterable[Foo]]:
...# foo.pyi
from __future__ import annotations
from typing import Protocol, Iterable, Union, runtime_checkable
from abc import ABCMeta
# What do I do? Redefine all of Foo's interfaces here?
@runtime_checkable
class FooProcessorT(Protocol, metaclass=ABCMeta):
def __call__(
self,
*roll_outcomes: "Foo", # <- undefined name 'Foo'
) -> Union["Foo", Iterable["Foo"]]: # <- undefined name 'Foo'
...What blows my mind is that this discovery makes the |
|
I found another case for which I cannot find a work-around, specifically where # test_case.py
from abc import abstractmethod
from typing import Any, Iterable, Protocol, Tuple, Union, runtime_checkable
class Foo:
__slots__ = ("bar", "baz")
def foo(self) -> None:
pass
@runtime_checkable
class SupportsFoo(Protocol):
__slots__: Union[
str,
Tuple[str],
Tuple[str, str],
Tuple[str, str, str],
Tuple[str, ...],
Iterable[str],
Any, # <-- adding this doesn't even help
] = ()
@abstractmethod
def foo(self) -> None:
pass
@runtime_checkable
class SupportsFooExactMatchWorks(Protocol):
__slots__: Tuple[str, str] = () # type: ignore
@abstractmethod
def foo(self) -> None:
pass
@runtime_checkable
class SupportsFooAnyWorks(Protocol):
__slots__: Any = ()
@abstractmethod
def foo(self) -> None:
pass
v1: SupportsFoo = Foo() # <-- error
v2: SupportsFooExactMatchWorks = Foo() # works: precise type match on __slots__
v3: SupportsFooAnyWorks = Foo() # works: __slots__: Any, and nothing else% mypy --config-file=/dev/null test_case.py
/dev/null: No [mypy] section in config file
test_case.py:48: error: Incompatible types in assignment (expression has type "Foo", variable has type "SupportsFoo")
test_case.py:48: note: Following member(s) of "Foo" have conflicts:
test_case.py:48: note: __slots__: expected "Union[str, Tuple[str], Tuple[str, str], Tuple[str, str, str], Tuple[str, ...], Iterable[str], Any]", got "Tuple[str, str]"
Found 1 error in 1 file (checked 1 source file)What this effectively means is that if Collecting related issues: #11821 (marked as fixed) |
__slots__ requires explicit type definition when used with Protocols__slots__ requires explicit type definition and must be Any (and nothing else) when used with Protocols
See python/mypy#11013 for details, but we may impose downstream heartache on those deriving from our protocols if we don't explicitly set the type of `__slots__` to `Any`. This corrects for my previously botched metaclass TypeVar in accordance with [this recommendation](python/mypy#9282 (comment)).
See python/mypy#11013 for details, but we may impose downstream heartache on those deriving from our protocols if we don't explicitly set the type of `__slots__` to `Any`. This *also* corrects for my previously botched metaclass TypeVar in accordance with [this recommendation](python/mypy#9282 (comment)). Could I have included that fix as a separate PR? Yes. Should I have? Yes. Did I? No. `#GottaGoFast` or something the kids say. (Translation: Because I'm lazy.)
See python/mypy#11013 for details, but we may impose downstream heartache on those deriving from our protocols if we don't explicitly set the type of `__slots__` to `Any`. This *also* corrects for my previously botched metaclass TypeVar in accordance with [this recommendation](python/mypy#9282 (comment)). Could I have included that fix as a separate PR? Yes. Should I have? Yes. Did I? No. `#GottaGoFast` or something the kids say. (Translation: Because I'm lazy.)
python/mypy#11013 is wreaking havoc with them.


The "fix" is to provide
Tuple[str, ...]as the type for__slots__:Shouldn't mypy already know the type of
__slots__?The text was updated successfully, but these errors were encountered: