Commit Graph

94 Commits

Author SHA1 Message Date
Shantanu
7e378a7554 PEP 584: add or operators to dict (#4671)
Co-authored-by: hauntsaninja <>
2020-10-15 13:42:01 +02:00
Shantanu
3a16ebb463 builtins: int can accept __trunc__-able (#4665)
Refer to https://docs.python.org/3/reference/datamodel.html#object.__trunc__
Fixes https://github.com/python/mypy/issues/9588

Co-authored-by: hauntsaninja <>
2020-10-14 09:47:47 +02:00
Sebastian Rittau
35f4ade96f Replace bool.__init__() with __new__() (#4614)
Fixes #4612
2020-10-09 00:20:03 +02:00
Sebastian Rittau
27dfbf68aa Use __new__ instead of __init__ for some builtin classes (#4555)
Closes #4514 #2630 #2686
2020-09-30 00:00:06 -07:00
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
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
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
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
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
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
Shantanu
ecb43149f7 builtins: NotImplemented is not callable (#4222)
Fixes #3315

Co-authored-by: hauntsaninja <>
2020-06-10 22:05:16 -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
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
Markus Pielmeier
b7d9a4a584 Add support for __pow__ with pow() builtin (#4109) 2020-05-30 15:38:19 -07: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
Shantanu
fd203e663e py39: add PEP 616 methods (#4090) 2020-05-27 19:24:25 -07:00
Shantanu
e857ad6ba9 zip: add some overloads for heterogeneous tuples (#3830)
Technically this is a lie, since we return a heterogeneous iterator, not
a tuple. But since we don't have a way of typing heterogeneous
iterators, this is the best we can do.

Fixes https://github.com/python/mypy/issues/8454
2020-05-03 21:02:26 -07:00
Peter Pentchev
0d7665d7b8 SyntaxError's filename and lineno may be undefined. (#3918)
The Python source (Objects/exceptions.c) explicitly checks for null
pointers before using the filename and lineno members. Some libraries,
e.g. pkg_resources, set filename and lineno to undefined values if
indeed none are appropriate.
2020-04-08 20:50:49 +02:00
Shantanu
6b4fd79808 builtins: add mod to various power functions (#3816) 2020-03-06 11:07:05 +01:00
Jelle Zijlstra
6a4044fd9b Make frozenset covariant (#3808)
The convariance was lost in #1057
2020-03-04 19:24:03 -08:00
Ran Benita
36c6f94de4 stdlib/2and3/builtins: change dict.fromkeys to classmethod (#3798)
The referenced issue in mypy is fixed.
2020-03-01 23:25:52 -08:00
Rune Tynan
e8f85d9479 Fix missing types in builtins/__builtin__ (#3705)
* Fix missing type in builtins/__builtin__

* Add return type to memoryview cast
2020-02-02 22:13:48 -08:00
Shantanu
70f0dc1491 builtins: fix bytearray.fromhex (#3691)
bytearray.fromhex is a classmethod, not a staticmethod
Mark positional-only args in the other fromhex's
2020-01-30 18:58:36 -08:00
Shantanu
9798c243c9 builtins: start can be passed by keyword to sum in 3.8 (#3692) 2020-01-30 18:56:50 -08:00
Ilaï Deutel
e7ddb21ae6 range.index() takes exactly one argument (#3668) 2020-01-27 20:19:12 -08:00
Shantanu
ed95668638 builtins.pow: improve annotation (#3647) 2020-01-24 09:20:46 +01:00
Shantanu
32563e9e1d builtins: mark positional-only args as such (#3648) 2020-01-23 15:35:45 +01:00
Shantanu
61600d6877 memoryview: add cast, obj attribute (#3598)
Fixes #3594
2020-01-09 15:57:17 -08:00
hauntsaninja
1efc1baaf7 stdtypes: update for py38 (#3580) 2020-01-06 20:57:34 -08:00
Jason
a1331accbe Update int.from_bytes to allow more than sequences (#3571)
`int.from_bytes` supports both iterables of ints and objects that define
__bytes__'.  As an example `int.from_bytes(iter([1, 0]), 'little'))`
returns 1.
2020-01-05 16:02:10 +01:00
hauntsaninja
5fdd6ad1a5 builtins.print: make sep and end Optional (#3468) (#3511)
The docs for Python 2 and Python 3 both explicitly mentions that None is a
valid value for sep and end.
https://docs.python.org/3/library/functions.html#print
2019-11-29 06:02:30 -08:00
robertschweizer
a9a4fd0d42 Add ImportError constructor arguments (#3512) 2019-11-29 06:01:30 -08:00
Michael Seifert
97ecd2b91f Change the return type of __reduce__ and __reduce_ex__ to Union[str, Tuple[Any, ...]] (#3453)
This allows subclasses to return strings (which are allowed) from these
methods without having mypy throwing an error.

Closes #3452
2019-11-10 15:48:56 +01:00
Alexander Schlarb
124111534a Add the memoryview.release method (#3405) 2019-10-25 14:29:44 +02:00