Commit Graph

61 Commits

Author SHA1 Message Date
Jaromir Latal
cd1a0c592a Add typestubs for unittest.util (#3186) 2019-08-27 15:34:22 +02:00
Shannon Zhu
72010bc5dc Update how mock classes alias to Any (#3182)
* 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
2019-08-19 17:10:27 -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
Sebastian Rittau
9ccf9356bf Remove Python 3.4 support (#3147)
Closes #3123
2019-07-27 10:58:21 +02:00
Jennifer Taylor
36b28e5b97 Split unittest stubs and add a few field (#2973) 2019-05-08 17:46:45 +02:00
Jan Szopinski
bf2d51742b unittest: Add overloads to assert(Not)AlmostEqual(s) (#2901) 2019-05-05 15:15:17 -04:00
Stephen Thorne
97240083c4 Correct the type signature of assertRaisesRegexp (#2926)
assertRaisesRegexp is the old name of assertRaisesRegex, they are the
same, just that the old one is deprecated.
2019-04-18 20:31:00 -07:00
Michael Lee
efb67946f8 Use variable annotations everywhere (#2909) 2019-04-13 10:40:52 +02:00
Masashi SHIBATA
c85fed8d98 Fix TextTestResult and TextTestRunner in unittest (#2910)
Co-Authored-By: c-bata <c-bata@users.noreply.github.com>
2019-04-12 12:50:12 +02:00
Savo Kovačević
eb6dbe510d Fix unittest.TestCase assert methods' argument names (#2724)
And fix assertNotAlmostEqual overloads
2019-01-05 17:31:32 -08:00
Juan Gonzalez
76a334daeb Complete unittest.TextTestResult stubs (#2700) 2018-12-21 08:37:33 +01:00
Adam Simpkins
c1dbdc7e26 Fix the return type for unittest.TestCase.run() (#2603)
In Python 3 this method returns None if the test is skipped, and a
TestResult otherwise.
2018-11-16 10:32:42 +01:00
Peter Pentchev
f8e5222c46 unittest.TestCase.assertRaises(): BaseException (#2594)
In Python 3, just as in Python 2, the expected exception argument to
assertRaises() and assertRaisesRegex() must be a subtype of
BaseException, not just of Exception.

Closes #2593
2018-11-08 18:42:51 -08:00
PRAJWAL M
13e84dc004 unittest: Complete assertRaisesRegex (#2568)
fixes #2523
2018-10-30 09:11:16 -07:00
Sebastian Rittau
0730fe5fcb Fix return of patch() and patch.multiple() (#2520)
* Fix return of patch() and patch.multiple()

* Update third party mock as well
2018-10-25 20:55:11 -07:00
Richard Levasseur
4122a70648 Add some private methods to unittest (#2494)
This adds a few stubs that are used by absl-py, and, without them, cause
type checker errors under Pytype:
 * TestCase._formatMessage
 * TestCase._testMethodName
 * TestCase._getAssertEqualityFunc
 * TestProgram.runTests
2018-10-01 20:18:26 -07:00
Dmitry Shachnev
c6c8d7e540 unittest: make assertIn/assertNotIn accept iterables (#2447)
Fixes #2381.
2018-09-11 18:48:44 +02:00
Jelle Zijlstra
8b9e4c378a fix module argument to unittest.main (#2403) 2018-09-10 11:04:05 -07:00
Bruce Merry
c541077ffb Accept AnyStr in unittest.assertRegex (#2315)
And also in assertNotRegex, assertRegexpMatches.

Closes #2312.
2018-07-09 13:23:18 -07:00
Sebastian Rittau
b05e99297c Drop Python 3.3 support from several stubs (#2266)
* Drop Python 3.3 support from importlib stubs

* Drop Python 3.3 support from html and symbol stubs
2018-06-20 16:46:11 -07:00
Jukka Lehtosalo
feeb4e71ef Change mock classes to Any values to avoid false positives (#2255)
The previous definitions in `mock` caused many false positives in
internal Dropbox repositories.

One source of problems was `List[Mock]` not being compatible with
`List[SomeClass]`, since `list` is invariant.
2018-06-19 16:28:19 +01:00
Guido van Rossum
af3b015aa8 mock.patch.object() attribute arg should be Text (#2210)
It works and is needed for code using the dreaded
'from __future__ import unicode_literals'.
2018-06-09 22:36:24 -07:00
Max R
34b926008e Remove Python 3.3 check in mock.pyi (#2201) 2018-06-05 15:30:28 -07:00
Jelle Zijlstra
eef0b1d562 fix type for TestCase.assertIn (#2186)
* fix type for TestCase.assertIn

This does essentially `assert member in container`, so we want a `Container`, not an `Iterable`.

This came up in 68e9d426a8..0bbee43d60 (r192525658).

* use any for assertIn
2018-06-04 15:34:09 -07:00
Robert Collins
98badb6eff Fix mock patch.dict stub and add mock stub (#2173)
The dict stub was referring to an instance, not the type, leading to
__call__ being considered when using as a decorator, rather than
__init__.

mock is a backport of the stdlib module and should be defined the same.
2018-05-31 21:45:54 -07:00
Jelle Zijlstra
37aba00fe8 fix using ZipFile as a ContextManager (#2043) 2018-04-12 12:29:22 -07:00
Jelle Zijlstra
7cfbc7d17f more 3.7 features (#2015) 2018-04-06 11:09:11 -07:00
rchen152
38dc8f5a6a Switch usages of mypy_extensions.NoReturn over to typing.NoReturn. (#1942)
* Change mypy_extensions.NoReturn to typing.NoReturn everywhere.
2018-03-05 12:42:29 -08:00
rchen152
066c434410 Improve the unittest stubs. (#1927)
* Improvements to stdlib/2/unittest.pyi

* Adds a bunch of missing things.
* Fixes the signatures of assertRaises and failUnlessRaises.
* Corrects the name of a TestCase subclass from CallableTestCase
  to FunctionTestCase.
* Makes defaultTestLoader an instance of TestLoader, as it should
  be, rather than an alias. Based on https://docs.python.org/2/library/unittest.html and
  https://github.com/python/cpython/tree/2.7/Lib/unittest.

* Improvements to stdlib/3/unittest/__init__.pyi

* The constructor arguments to TextTestResult are all required.
* There is a module-level load_tests() function.
* removeResult() returns a bool. Based on https://docs.python.org/3/library/unittest.html and
  https://github.com/python/cpython/tree/master/Lib/unittest.
2018-02-27 06:46:10 -08:00
Nick Gaya
3e85d6bb61 Add missing unittest.mock.patch attributes (#1882) 2018-02-16 09:13:33 -08:00
rchen152
66e5863ead Change the return type of unittest.TestCase.fail() to NoReturn (#1843)
* Change the return type of unittest.TestCase.fail() to NoReturn

* Import mypy_extensions.NoReturn instead of typing.NoReturn
2018-01-26 14:28:37 -08:00
Jelle Zijlstra
83ca997140 Change more defaults to ... (#1729)
* codemod ': Any = None' ': Any = ...'
* codemod ': (Union|Optional)([^=]+)\s+=\s+-?\d+' ': \1\2 = ...'
* codemod ': (Union|Optional)([^=]+)\s*=\s*-?(None|False|True)' ': \1\2 = ...'
* codemod ': (int|float|bool)\s*=\s*-?\d+' ': \1 = ...'
* codemod ': (bool)\s*=\s*(False|True)' ': \1 = ...'
* codemod ': Any\s*=\s*(False|True|None)' ': Any = ...'
2017-11-13 06:56:24 -08:00
Jelle Zijlstra
54dd6ba27c Change numerous default values to ... (#1722)
This is the convention, and some default values (e.g. strings) apparently
break pytype.
2017-11-09 06:28:40 -08:00
Sebastian Rittau
87ef6b8d53 Add stub for unittest.mock.patch.multiple() (#1686)
* Add stub for unittest.mock.patch.multiple()

* Use ... for default arguments in unittest.mock.patch() et al.

* Tighten type of create argument to patch() et al.
2017-10-26 00:57:56 -04:00
Jelle Zijlstra
78587dc895 Revert "make __class__ refer to the current object's class (#1549)" (#1632)
This reverts commit 1a164b630c.

Reverts python/typeshed#1549.

See Guido's comments in the original PR.
2017-09-29 11:37:54 -07:00
Jelle Zijlstra
1a164b630c make __class__ refer to the current object's class (#1549)
Fixes python/mypy#3061
2017-09-27 18:11:59 -07:00
Martin DeMello
d389ef3d85 Remove a few files from the pytype blacklist. (#1628)
Fixes the following issues:

* Literals rather than ... for default values
* None rather than ... for default value of typed variable
* Literals rather than ... # type for top level constants
* # Foo rather than # type: Foo
* return value of init not set to None
2017-09-27 07:57:13 -07:00
Guido van Rossum
76685480e0 Misc fixes found by running mypy against Dropbox internal codebase "C". (#1575) 2017-08-25 20:34:13 -07:00
toejough
f6b013ba52 ignore type for mock.pyi inheritance from Any (#1492)
It is currently required to shut up mypy when run with `--strict`
or `--disallow-subclassing-any`. The `Any` base class is currently
the only way to allow passing an instance of `Mock` to functions
expecting other classes (as is Mock's purpose).
2017-07-19 11:33:59 -07:00
Jelle Zijlstra
ac651d2f8c Fix a few TODOs (#1487)
* fix some easy TODOs

* fix io
2017-07-19 20:27:50 +03:00
Guido van Rossum
350563223f Add Optional[] for all remaining cases of x: <type> = None (#1424)
* Final round of adding Optional[] to type of arguments with default = None
* Update Travis to use --no-implicit-optionals and clarify CONTRIBUTING.md
2017-06-21 10:50:21 -07:00
Carl Meyer
802f3b21b3 Fix __init__ signature for unittest.mock.MagicMock. (#1388) 2017-06-04 10:08:56 -07:00
Benjamin Starling
9abbc0d392 Fix unittest.mock.Mock keyword arguments to address #1282 (#1300) 2017-05-22 23:30:40 -07:00
Jelle Zijlstra
7dd2f80194 Fixes to ContextManager (#1249)
* add typing.ContextManager for 3.6+ only

This fixes the easier part of #655.

Would it make sense to add a generic typing.ContextManager that exists in any Python version?

* update comment

* fix argument types for ContextManager.__exit__

* add AsyncContextManager

* add @asynccontextmanager

* typing.ContextManager now always exists

* back out async-related changes

Will submit those in a separate PR later

* fix import order

* AbstractContextManager only exists in 3.6+

* AbstractContextManager -> ContextManager
2017-05-08 16:21:51 -07:00
Roy Williams
452c1e1d78 Fix patch.object to return a _patch context manager. (#1142)
* Fix patch.object to return a _patch context manager.

This should fix https://github.com/python/typeshed/issues/914

* Prefer None over ... to be consistent with the rest of the file.
2017-04-06 18:47:28 -07:00
Guido van Rossum
1ea3d2de57 Make all single-constraint TypeVars use bound= insteads (plus hack) (#1118)
Another attempt for #804, to unblock python/mypy#2626.
There's a work-around here for python/mypy#1551.
2017-03-29 14:59:24 -07:00
jkleint
433b1b2ebc TestCase.assertSetEqual() should accept frozenset (#1063)
This declares the method to take the union of Set and FrozenSet
rather than AbstractSet since that is where `difference()`
(used by `assertSetEqual()`) is defined.
2017-03-21 23:10:19 -07:00
Jukka Lehtosalo
f296b37e80 Fixes to unittest stubs (#1024)
* Fixes to unittest stubs

* Make types more precise
2017-03-18 14:35:33 -07:00
jkleint
17be26165d Relax type of TestCase.assertCountEqual() params from Sequence to Iterable (#1006)
The stubs for `unittest.TestCase.assertCountEqual()` specify the parameters are Sequences; in fact the method works fine with Iterables, as the first thing the method does is convert them to lists.  [1]  The docs do say sequences, but it appears there is no reason they cannot be Iterables.

[1]: https://github.com/python/cpython/blob/master/Lib/unittest/case.py#L1156
2017-03-15 22:25:20 -07:00
Sebastian Meßmer
85866fe1b9 Fix unittest/mock.pyi (#973)
* Fix unittest/mock.pyi

Add a lot of missing types

* Fix formatting

* Make two and from_kall bool

* Further style fixes

* style fixes
2017-03-08 06:50:28 -08:00