This is important because mypy doesn't generally think functions are compatible with `FunctionType`, so `inspect.getsource` on arbitrary functions is rejected by the current annotations.
This was discussed in #3181 and the correct explanation was given that
object ought to be preferred here. In practice this caused an obscure
issue with mypy when using this operations on the results of
six.viewkeys(), since it decided that it wanted a mapping *from* object
as a result of inference contexts. Grumble.
* Update how mock classes alias to Any
> First, the z: Any situation looks like a bug or accidental feature to me.
This is definitely meant (and works) as a variable declaration; that it
also allows using z as a type seems wrong. I can't find any evidence in
PEP 484 that this was intended; in mypy it's likely the accidental result
of other design choices meant to shut up errors about Any.
Ideally these classes could be declared as empty class stubs, but since the comments suggest this isn't possible yet, let's update these to be type aliases to Any rather than global variables of type Any. This would avoid invalid type errors when the implementation of type checkers respect the intention that `z: Any` does not make `z` a valid type.
* Update mock.pyi
This pull request is a follow-up to https://github.com/python/mypy/issues/7214.
In short, within that mypy issue, we found it would be helpful to
determine between contextmanagers that can "swallow" exceptions vs ones
that can't. This helps prevent some false positive when using flags that
analyze control flow such as `--warn-unreachable`. To do this,
Jelle proposed assuming that only contextmanagers where the `__exit__`
returns `bool` are assumed to swallow exceptions.
This unfortunately required the following typeshed changes:
1. The typing.IO, threading.Lock, and concurrent.futures.Executor
were all modified so `__exit__` returns `Optional[None]` instead
of None -- along with all of their subclasses.
I believe these three types are meant to be subclassed, so I felt
picking the more general type was correct.
2. There were also a few concrete types (e.g. see socketserver,
subprocess, ftplib...) that I modified to return `None` -- I checked
the source code, and these all seem to return None (and don't appear
to be meant to be subclassable).
3. contextlib.suppress was changed to return bool. I also double-checked
the unittest modules and modified a subset of those contextmanagers,
leaving ones like `_AssertRaisesContext` alone.
I realized while working on srittau/type-stub-pep#64 that a
few things we do in existing enum definitions in typeshed are
problematic:
- Using "= ..." doesn't allow type checkers to correctly type the
result of Enum.MEMBER.value. In fact, mypy at least infers
.value to be "Ellipsis" if you do this.
- Properties on the enum values themselves, like HTTPStatus.phrase,
should not be specified directly as attributes, because it makes
type checkers think that the properties themselves are enum
members.
I ended up doing a bit more cleanup to the signal module:
- Remove unnecessary ... initializers.
- Remove unnecessary _SIG = Signals alias.
- I don't have Windows to test, but the C code for _signal suggests
that CTRL_C_EVENT and CTRL_BREAK events are not Signals, but just ints:
1dbd084f1f/Modules/signalmodule.c (L1575)
The stub for asyncio.as_completed declared it as taking a Sequence of
Futures. This was unnecessarily strict.
Just like asyncio.wait, asyncio.as_completed can be declared to take
an Iterable of Futures instead. Both these functions iterate over
their argument only once to store its items in a set, so an Iterable
is sufficiently strict. This has been true since the initial
implementation of the functions.