Commit Graph

72 Commits

Author SHA1 Message Date
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
Eric N. Vander Weele
966f8d24e6 Revert __import__ function annotation to return type back to Any (#3383)
From python/mypy#7582.

This partially reverts back the change in
0ee7c3c38b to have `__import__` return
`Any` instead of `ModuleType`.
2019-10-18 00:10:41 +02:00
Sebastian Rittau
b969ead0ce Reorder memoryview.__setitem__ overloads (#3365)
Necessary for python/mypy#7717
2019-10-15 14:14:48 +02:00
Sebastian Rittau
8a7d61741d Python3.8 additions and changes (#3337)
* Add as_integer_ratio() to a few types

* Add dirs_exist_ok to copytree()

* int, float, complex accept __index__ args

Also fix complex.__init__ argument names

* Add __reversed__ to dict et al.

* Python 3.8 date(time) arithmetic fixes

* Add CodeType.replace()
2019-10-10 20:51:27 -07:00
Sebastian Rittau
07c8675ba5 Remove unused # type: ignore comments (#3325) 2019-10-09 10:27:18 -07:00
wouter bolsterlee
824e94a933 Make 2-arg iter() retrun type match passed callable's return type (#3326)
This is a continuation of #3291, which was the initial fix for #3201.

The 2-arg version of iter() turns a callable into an iterator. The
changes made in #3291 introduce an Any return type for both the
callable's return type and the iterator's type, while in reality the
return type of the function is always the same as the iterator's type.
2019-10-09 08:44:41 +02:00
sinancepel
cd418e9546 introduce __floor__ and __ceil__ for int in Python 3 (#3306) 2019-10-04 19:16:32 -07:00
Utkarsh Gupta
fa571fb224 builtins.pyi: Use two type vars (#3291)
Fixes: #3201
2019-10-04 00:15:41 +02:00
Sebastian Rittau
c32e1e2280 Enable --disallow-any-generics for stubs (#3288) 2019-10-01 05:31:34 -07:00
Eric Traut
468d13a205 Modified __add__ method in tuple class (#3252)
Modified __add__ method in tuple class to allow it to accept tuples with different generic parameter types. This allows, for example:
a = (1, )
b = a + (2.4, )
2019-09-30 12:42:40 +02:00
Guido van Rossum
b336182b69 Fix some errors with --disallow-any-generics (#3276)
See #3267. Covers all of stdlib/2and3.
2019-09-29 09:15:27 -07:00
sinancepel
01b7002a89 Introduce explicit __str__ and __repr__ for Exceptions (#3247) 2019-09-23 18:25:49 +02:00
Benjamin Peterson
3fc8aec425 Allow passing str to Python 2's bytearray.extend. (#3223) 2019-09-04 23:24:58 +02:00
Sebastian Rittau
efb4af0108 Mark some types as non-hashable (#3219)
Based on @JelleZijlstra's PR #2221.

Fixes #2148
2019-09-04 17:38:09 +02:00
Ran Benita
b2cd972b17 builtins: int.__pow__ can take a modulo argument (#3192)
See:
https://docs.python.org/3/reference/datamodel.html#object.__pow__
https://docs.python.org/3/library/functions.html#pow
2019-08-17 07:23:53 -07:00
Ran Benita
c7417e8b3f builtins,numbers: harmonize float.__round__ and Real.__round__ (#3193)
See:
https://docs.python.org/3/reference/datamodel.html#object.__round__
https://docs.python.org/3/library/functions.html#round
2019-08-17 07:23:04 -07:00
Ran Benita
c0625d3280 builtins: add __trunc__ to int and float (#3194)
See:
https://docs.python.org/3/reference/datamodel.html#object.__trunc__

It also works on Python 2.7.
2019-08-17 07:22:03 -07:00
Michael Lee
b294782183 Make most contextmanager __exit__ signatures return Optional[bool] (#3179)
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.
2019-08-16 16:13:33 -07:00
Jon Dufresne
0ee7c3c38b Update __import__ function annotations (#3188)
Per the docs, globals/locals is an optional argument.
Additionally, globals/locals can be any mapping type, not only a dict.
Likewise, fromlist can be any sequence (the docs mention a tuple, not a
list).
The function returns a ModuleType, not Any.
2019-08-16 11:03:00 +02:00
Phil Jones
1dc24e1c67 Add nbytes to the memoryview definition (#3167)
`nbytes`, like `contiguous`, was added in Python 3.3.
2019-08-03 07:51:55 -07:00
Ran Benita
c8e7d98c1f Allow function.__code__ in Python 2 (#3152)
The following code works:

    >>> print(sys.version)
    2.7.16 (default, Mar 11 2019, 18:59:25)
    >>> def f(): pass
    >>> print(f.__code__)
    <code object f at 0x7f8534ecc8a0, file "<stdin>", line 1>
    >>> isinstance(f.__code__, types.CodeType)
    True

but it didn't type-check with `mypy --python-version 2.7`.
2019-07-26 07:40:22 -07:00
秋葉
e2ec5d0525 Make SyntaxError.text be optional (#3119)
Closes #3118
2019-07-15 09:11:45 +02:00
Brandt Bucher
34b47101dd Slice attributes can be of any type. (#3024) 2019-07-11 12:13:32 -07:00