Commit Graph

629 Commits

Author SHA1 Message Date
Shantanu
076983eec4 PathLike: make runtime_checkable (#4582)
I made PathLike a protocol in #4447, but it should also be
runtime_checkable.

Caught by mypy_primer:
src/werkzeug/utils.py:646: error: Only @runtime_checkable protocols can be used with instance and class checks
2020-09-27 07:50:56 +02:00
Shantanu
d6d2249edc builtins: rename _LT (#4579)
Having an obscure type variable name is causing some pretty inscrutable
errors. For instance:
```
xarray/core/utils.py:466: error: Value of type variable "_LT" of "sorted" cannot be "K"
tornado/simple_httpclient.py:324: error: Value of type variable "_LT" of "min" cannot be "Optional[float]"
```

I think having a more descriptive type variable name here is better for
user experience and helps address the "why" of an error.
2020-09-26 07:33:26 +02:00
proost
b1865639c2 set difference strictly (#3886)
Fixes #1840
2020-09-17 17:49:07 +02:00
Cebtenzzre
1334840323 Make os.fchdir, os.fsync, and os.fdatasync accept FileDescriptorLike (#4544)
For the fd passed to these functions, CPython accepts not just an int,
but also anything with a fileno() method.

Fixes #4539
2020-09-15 16:30:34 -07:00
Shantanu
dc0b4262c3 builtins: minor improvements (#4535)
Co-authored-by: hauntsaninja <>
2020-09-13 18:08:48 +02:00
Jake Bailey
f0bbc3bf2d Add special case for integers raised to a power of 2 (#4473) 2020-09-04 21:05:45 -07:00
Jérome Perrin
26ca4e75d8 subprocess: make env accept None also on python2 (#4504)
This was made for python3 in https://github.com/python/typeshed/pull/1291 but
this is also the case on python2
2020-08-30 20:58:20 -07:00
Jelle Zijlstra
5f9fd3d127 upgrade black version (#4486)
Manually removed a number of trailing commas to prevent black from unnecessarily
exploding some collections.
2020-08-26 18:36:01 +02:00
Akuli
c065982b7a support anything with .keys() and __getitem__ in dict.__init__ (#4470) 2020-08-24 13:03:38 +02:00
Shantanu
b438ccc3bc PathLike: change to Protocol (#4447)
Co-authored-by: hauntsaninja <>
2020-08-16 05:15:51 -07:00
Jukka Lehtosalo
276d0428b9 Avoid false positivies from urlparse (Python 2) (#4437)
PR #3887 changed some `str` types to `Union[str, unicode]`.  The `str`
types were too narrow, but the new types are too general, resulting in
around 80 false positives in a Dropbox internal repository.

I think that it's better to narrow down the types back some to avoid
the false positives, as these are commonly used functions.

Test cases:

```
from urlparse import urlunparse, urlunsplit, urljoin, urlparse, urlsplit

reveal_type(urlunparse(('1', '2', '3', '4', '5', '6'))) # str
reveal_type(urlunparse(['1', '2', '3', '4', '5', '6'])) # str
reveal_type(urlunparse((u'1', '2', '3', '4', '5', '6'))) # unicode
reveal_type(urlunparse([u'1', '2', '3', '4', '5', '6'])) # unicode

reveal_type(urlunsplit(('1', '2', '3', '4', '5'))) # str
reveal_type(urlunsplit(['1', '2', '3', '4', '5'])) # str
reveal_type(urlunsplit((u'1', '2', '3', '4', '5'))) # unicode
reveal_type(urlunsplit([u'1', '2', '3', '4', '5'])) # unicode

reveal_type(urljoin('x', 'y')) # str
reveal_type(urljoin(u'x', 'y')) # unicode
reveal_type(urljoin('x', u'y')) # unicode

reveal_type(urlparse('x').path) # str
reveal_type(urlparse(u'x').path) # str
reveal_type(urlparse(u'x').username) # Optional[str]

reveal_type(urlsplit('x').path) # str
reveal_type(urlsplit(u'x').path) # str
```
2020-08-11 13:03:06 +01:00
Jukka Lehtosalo
866b0c3bf0 ConfigParser: accept readline() that takes no arguments (#4433)
At runtime readline() is called without arguments, so requiring
an optional argument may result in false positives.
2020-08-10 15:55:32 +01:00
Jukka Lehtosalo
0cd2350595 Add back md5.md5 (#4432)
This was accidentally removed in #4287.
2020-08-10 12:52:13 +01:00
Eric Traut
f46fb7ff59 Added some missing type annotations in stdlib stubs. (#4418)
Co-authored-by: Eric Traut <erictr@microsoft.com>
2020-08-08 20:49:37 +02:00
Adam Hitchcock
3b6925b955 mark deque.rotate arg as having a default (#4386) 2020-08-05 17:35:22 -07:00
wouter bolsterlee
83e955b52f Use correct return type annotation for BaseException.with_traceback (#4298)
The return type of the BaseException.with_traceback() method [1] is not
specific enough. The return type is guaranteed to be of the same type as
‘self’, which is usually a subclass of BaseException.

In fact, .with_traceback() returns ‘self’:

    try:
        raise ValueError
    except Exception as exc:
        assert exc.with_traceback(None) is exc

Fix the annotation to reflect this using the self-type annotation
technique described in PEP484 [2], which is supported by (at least)
mypy [3].

[1] https://docs.python.org/3/library/exceptions.html#BaseException.with_traceback
[2] https://www.python.org/dev/peps/pep-0484/#annotating-instance-and-class-methods
[3] https://github.com/python/mypy/issues/1212
2020-06-30 11:40:25 -07:00
Jelle Zijlstra
0142a87da8 adjust isort config (#4290)
Fixes #4288.

- Default imports to THIRD_PARTY, so in effect we merge the FIRST_PARTY and THIRD_PARTY stubs. This means import order is no longer affected by whether typing_extensions is installed locally.
- Treat typing_extensions, _typeshed and some others as standard library modules.

Note that isort master is very different from the latest release; we'll have to do something
different if and when the next isort release comes out.
2020-06-29 00:00:21 -07:00
Jelle Zijlstra
5d553c9584 apply black and isort (#4287)
* apply black and isort

* move some type ignores
2020-06-28 13:31:00 -07:00
Sebastian Rittau
564a8220f4 Add float.__ceil__ and __floor__ (#4274)
Closes: #3195
2020-06-26 22:26:39 -07:00
Shantanu
fc8b0ed94e builtins: remove __class_getitem__ from type (#4270)
Co-authored-by: hauntsaninja <>
2020-06-26 12:45:22 +02:00
Jia Chen
92fa4e799c Using dunder parameter name in builtins._SupportsLessThan (#4264) 2020-06-25 21:02:00 +02:00
Shantanu
70459abb44 Revert "zip: add some overloads for heterogeneous tuples (#3830)" (#4254)
This reverts commit e857ad6ba9.

Co-authored-by: hauntsaninja <>
2020-06-25 08:46:02 -07:00
helafumpek
534ebd5051 Change argument type in random.shuffle (python/typeshed#4255) (#4260)
Fixes #4255
2020-06-24 08:17:13 +02:00
Bruce Merry
fb398b1d59 Use the ReadableBuffer type in more places (#4245)
This is a follow-up on #4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in #4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
2020-06-22 06:17:24 -07:00
Milap Sheth
66a9a4b5ce Fix type hint of generator throw method (#4253)
* Fix type hint of generator throw method

* Incorporated changes from #4252
2020-06-21 15:23:28 -07:00
Luciano Ramalho
1350e710cc max, min overloads with Protocol: fix #4051 (#4227) 2020-06-19 12:37:43 +02:00
Sebastian Rittau
51cf2f51b8 Add IO protocols to _typeshed (#4230) 2020-06-14 20:44:48 +02:00
Jelle Zijlstra
d0b85d7efe remove _types (#4220) 2020-06-11 09:10:52 +02:00
Shantanu
ecb43149f7 builtins: NotImplemented is not callable (#4222)
Fixes #3315

Co-authored-by: hauntsaninja <>
2020-06-10 22:05:16 -07:00
Jelle Zijlstra
43e93f803f use _typeshed's Path aliases (#4214) 2020-06-10 20:57:09 -07:00
Jelle Zijlstra
ea577cec1f move OpenTextMode and friends to _typeshed (#4213) 2020-06-10 15:36:21 +02:00
Mikhail Golubev
85281b636e Add __class_getitem__ to builtins generified in PEP 585 (#4184)
* Add __class_getitem__ to builtins generified in PEP 585

* Declare types.GenericAlias and use it in __class_getitem__
2020-06-09 20:10:07 -07:00
Luciano Ramalho
bb9e1a649c using type var to work around List invariance (#4192) 2020-06-06 18:32:15 -07:00
Dakkaron
3438cb92e1 pipes.quote takes AnyStr (#4173) 2020-06-04 12:37:57 +02:00
Sebastian Rittau
a913af9523 flake8: Enable F811 (#4158) 2020-06-02 14:08:54 -07:00
Jelle Zijlstra
adeda24fce open: introduce concrete return types (#4146)
* make io classes inherit from typing IO classes

This makes these classes usable if type annotations are given as "IO"
or "TextIO". In the future, we'll then be able to move open() to
return a concrete class instead (#3951).

* open: introduce concrete return types

Fixes #3951.

We use the values of the "mode" and "buffering" arguments to figure out
the concrete type open() will return at runtime. (Compare the CPython
code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.)
2020-06-01 00:48:12 +02:00
Rune Tynan
d863210335 Improve TypeVar stub (#4150) 2020-05-30 20:27:59 -07:00
Markus Pielmeier
b7d9a4a584 Add support for __pow__ with pow() builtin (#4109) 2020-05-30 15:38:19 -07:00
Shantanu
de2c2947fe windows: fix splitunc param name (#4143)
Co-authored-by: hauntsaninja <>
2020-05-29 20:37:19 -04:00
Ilaï Deutel
846d922df2 More precise return types for open(), Path.open(), bz2.open(), etc. (#3371)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
2020-05-28 09:20:23 -07:00
Jelle Zijlstra
0cffa59e57 use a Protocol for str.format_map() (#4122)
Fixes #3824
2020-05-28 13:27:57 +02:00
Rune Tynan
712f6d3709 Fix winreg pos-only args and parameter names (#4117)
* Fix winreg pos-only args and parameter names

* Fix consistency check
2020-05-27 20:22:32 -07:00
Brad Solomon
ddb47deb27 Allow Union[unicode, str] rather than just str in several places (#3887)
* Allow unicode objects throughout urlparse.py

Functions such as `urlparse()`, if given a unicode object,
will happily return a ParseResult of unicode components.

Relatedly, functions like `unquote()` will accept any of
bytes/str/unicode and return an object of the same type
or a composite containing that type.

* Allow unicode in several places in Thread

`name` and `kwargs` to Thread.__init__ function perfectly
well with unicode, not just str.

Note: the .name attribute will always be str even
if the constructor is passed something else, since
__init__ calls:

    self.__name = str(name or _newname())

* Use typing.AnyStr properly

...rather than defining a new TypeVar unncessarily.

* Use typing.Text properly

Text is behaviorally equivalent to Union[str, unicode]
for Python 2 argument types.

* Remove outdated import & definition

* [check file consistent] copy changes to _dummy_threading.pyi
2020-05-27 19:52:14 -07:00
Shantanu
fd203e663e py39: add PEP 616 methods (#4090) 2020-05-27 19:24:25 -07:00
Rune Tynan
fcdfacf944 Fix stubtest failures for socketserver on windows (#4103) 2020-05-27 19:34:35 +02:00
Rune Tynan
e49c156d92 Add winreg stubs (#3794) 2020-05-27 17:15:08 +02:00
Rune Tynan
5e80ca9e44 Fix stubtest failures for path files on windows (#4096) 2020-05-27 09:39:26 +02:00
Nguyễn Gia Phong
0aa9060e66 Remove context manager hints in multiprocessing.pool in Python 2 (#4060) 2020-05-23 22:11:19 +02:00
Rebecca Chen
037377f502 Mark the arguments to os.killpg as positional-only in Python 2. (#4033)
This was already done in Python 3. In Python 2 as well, this method does
not take keyword arguments.
2020-05-17 18:06:35 -07:00
Selim Belhaouane
3ac1f86499 Change return annotation of Popen.poll to Optional[int] (#3986)
Fixes #3984
2020-05-13 10:18:25 -07:00