You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
__slots__ Union type should not include None. #1853
def all_slots(type: Type) -> Set[str]:
'''
Subclasses of slots classes may define their own slots,
which hold just the additions to the parent class.
Therefore we need to iterate over the inheritance chain to get all slot names.
We use __mro__ here, and hope for the best regarding multiple inheritance.
'''
slots: Set[str] = set()
for t in type.__mro__:
try: s = t.__slots__
except AttributeError: break
else:
if isinstance(s, str):
slots.add(s) # single slot.
else:
slots.update(cast(Iterable[str], s)) # mypy bug: __slots__ cannot be None.
return slots
Without the cast, using latest mypy/typeshed, I get: Argument 1 to "update" of "set" has incompatible type "Optional[Iterable[str]]"; expected "Iterable[str]"
Mypy thinks that the type of __slots__ is Union[str, Iterable[str], None] (it eliminates the str case due to the check above).
However, I don't think __slots__ can legally be None:
class Z: __slots__ = None
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
Looks like you're right -- please submit a fix to remove the Optional[]. There's no way in typeshed (or mypy) to express "this may or may not exist" and mypy does not help detecting cases where that may be an issue, but Optional[] is definitely not the right way to spell that.
It does seem like there should be a generic type understood by Mypy as “possibly an attribute”. I’ve run into this a number of times and had to refactor code in order to express correct type annotations. Usually the refactoring makes the code better but it seems like static checkers should be able handle those idioms. Should this be opened against mypy or std typing?
George
On Feb 26, 2018, at 3:05 PM, Guido van Rossum ***@***.***> wrote:
Looks like you're right -- please submit a fix to remove the Optional[]. There's no way in typeshed (or mypy) to express "this may or may not exist" and mypy does not help detecting cases where that may be an issue, but Optional[] is definitely not the right way to spell that.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
On Feb 26, 2018 16:09, "George King" ***@***.***> wrote:
It does seem like there should be a generic type understood by Mypy as
“possibly an attribute”. I’ve run into this a number of times and had to
refactor code in order to express correct type annotations. Usually the
refactoring makes the code better but it seems like static checkers should
be able handle those idioms. Should this be opened against mypy or std
typing?
George
> On Feb 26, 2018, at 3:05 PM, Guido van Rossum ***@***.***>
wrote:
>
> Looks like you're right -- please submit a fix to remove the Optional[].
There's no way in typeshed (or mypy) to express "this may or may not exist"
and mypy does not help detecting cases where that may be an issue, but
Optional[] is definitely not the right way to spell that.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or mute the thread.
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1853 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMo_orIGaZr3XmHmM6T2PyXWWVLMfks5tY0fJgaJpZM4R3mLj>
.
Activity
asottile commentedon Feb 26, 2018
I think what the annotation meant by
Optionalis it's sometimes not present -- though I don't know of a way to express that in mypy.For context, here's where this was added: #780
gvanrossum commentedon Feb 26, 2018
Looks like you're right -- please submit a fix to remove the
Optional[]. There's no way in typeshed (or mypy) to express "this may or may not exist" and mypy does not help detecting cases where that may be an issue, butOptional[]is definitely not the right way to spell that.gwk commentedon Feb 27, 2018
gvanrossum commentedon Feb 27, 2018
remove Optional from type of __slots__
remove Optional from type of __slots__ (#2128)
remove Optional from type of __slots__ (python#2128)