Commit Graph
70 Commits
Author SHA1 Message Date
Jukka Lehtosalo 7cd533e6a4 Fix unnecessary use of type variable in max() (#6584) 2021-12-14 07:43:06 -08:00
Jukka Lehtosalo 78806f5402 Use AbstractSet instead of set in random and inspect (#6574) 2021-12-13 17:26:28 +01:00
Jukka Lehtosalo 8a5d91ca37 Relax signature of logging.config.loadConfig (#6577)
I did a a cursory investigation using GitHub search and also looked at
a big internal codebase, and a significant fraction of callsites used
a dict type instead of a TypedDict or a dict literal.

It seems that it's a common use case to store the config within an
attribute. For example, something like this:

```
CONFIG = {
   ...
}

...
logging.config.dictConfig(CONFIG)
```

Another use case that was not properly supported is reading the
config from a file, and the config is given `dict[str, Any]` as
the type.

Mypy can still do some type checking of the argument if called
with a dict literal, so I feel this is a reasonable compromise
between type checking strictness and usability.
2021-12-13 07:31:15 -08:00
Jukka Lehtosalo a7798f6060 Revert "lru_cache preserves signature of wrapped function (#6221)" (#6356)
This reverts commit 8bda66a737.

The change causes issues with ParamSpec implementations in type
checkers, at least pyright and my work-in-progress support for
ParamSpec in mypy. It's not yet clear how to fix the issues, so I
think that it's best to revert this, at least temporarily until we've
found a good solution. See #6347 for context.
2021-11-22 12:52:27 +01:00
Jukka Lehtosalo 25649bc1e5 Add getattr overload variants to help mypy type inference (#6355)
These silence errors about missing type annotations for calls
like these:

```
x = getattr(o, 'a', [])
y = getattr(o, 'b', {})
```

This is basically a generalization of #5518 and other overloads we already
have.

This works around python/mypy#11572. I encountered the issue in several
places when testing recent typeshed against an internal repo.
2021-11-22 11:08:40 +00:00
Jukka Lehtosalo 25ba2bad06 logging: Make LoggerAdapter and StreamHandler generic in Python 2 (#6330)
Without this writing straddling code is quite tricky, as these are
generic in Python 3.
2021-11-17 16:28:44 +00:00
Jukka Lehtosalo bd416902f7 Fix annotation in selenium.webdriver.remove.webdriver (#5859)
Docstring description of the argument includes this text:

"Either a string representing URL of the remote server or
a custom remote_connection.RemoteConnection object."
2021-08-06 17:25:25 +01:00
Jukka Lehtosalo 1ad4b79139 Stubs for selenium (#5792)
These were generated using stubgen, with manual fixes. I tried to remove
exported names that are clearly not part of the public interface, but in many
cases this wasn't obvious so I left the exports, just in case.

I had to do major changes to exported names in `selenium.webdriver` to 
make it work as expected (this was originally implemented by @ilevkivskyi 
in our internal stubs).

Selenium has been working on adding inline type annotations, but it's not clear
when these will be released. The release will be 4.x, and having stubs for 3.x 
could still be valuable for users on older versions.
2021-07-24 13:43:24 -07:00
Jukka Lehtosalo 2d3bde439e Stubs for mysqlclient (#5791) 2021-07-19 10:28:00 +02:00
Jukka Lehtosalo a0f199727b Stubs for psycopg2 (#5783) 2021-07-15 17:29:56 +02:00
Jukka Lehtosalo 937d31df69 Stubs for jsonschema (#5784) 2021-07-15 17:28:57 +02:00
Jukka Lehtosalo 6d7159c822 Stubs for prettytable (#5782) 2021-07-15 14:34:59 +02:00
Jukka Lehtosalo 2a4cc03307 Add stubs for jmespath (#5780) 2021-07-15 13:26:53 +02:00
Jukka Lehtosalo b3b8daed2e Fix import sorting in create_baseline_stubs script (#5781)
Run isort before black since changes made by isort don't follow stub
conventions.
2021-07-15 13:24:46 +02:00
Jukka Lehtosalo e2fe224b52 Stubs for psutil (#5776)
I generated these using stubgen and made various manual tweaks to fix issues
reported by stubtest.

Some of the submodules with underscore prefixes are used by open source code,
so I'm including them.

Various definitions are platform-specific. I added some sys.platform checks, but
it's hard to get these right. We may need to iterate on them later.
2021-07-15 11:37:53 +01:00
Jukka Lehtosalo d85c54dc47 Add simple tool for creating baseline stubs (#5777)
This tool is a simple wrapper around stubgen and other tools that simplifies the
creation of "baseline" stubs with minimal annotation coverage. These stubs allow
basic type checking (e.g. number of arguments, whether X is valid as a base class),
and once we have baseline stubs, it's easy to contribute incremental changes that
add type annotations.

Here's a summary of what the tool does:
1. Check that the package is installed
2. Run stubgen
3. Copy generated stubs to the correct directory under `stubs/`
4. Run black
5. Run isort
6. Generate basic metadata with correct package version
7. Update pyright exclusions (needed since there are missing types)
8. Print suggestions about next steps needed to contribute stubs to typeshed

For example, to generate stubs for `iso8601`, you can run it like this:
```
python scripts/create_baseline_stubs.py iso8601
```

Sometimes the project name is different from the runtime package name. In
this case run the tool with `--package <runtime-package>`.
2021-07-15 11:22:28 +01:00
Jukka Lehtosalo e5459900c0 Make html, ipaddress, reprlib and winreg only available on Python 3 (#5502)
None of these are available in Python 2.7.
2021-05-19 12:16:10 +01:00
Jukka Lehtosalo b08a2e6dea Fix Python 2 genericpath.commonprefix (#5500)
The more specific overload item should come first. Also
fix the return type.
2021-05-19 12:56:18 +02:00
Jukka Lehtosalo 17bc1d83ea Don't import enum in Python 2 stubs (#5445)
Python 2.7 doesn't have enum in the stdlib, in particular, so we
shouldn't import it.
2021-05-14 15:29:54 +02:00
Jukka Lehtosalo a5e243fbcd Change some str types to Text in google.protobuf.text_format (#5343)
Python 2 `unicode` values are supported. For example:
https://github.com/protocolbuffers/protobuf/blob/master/python/google/protobuf/text_format.py#L843
2021-05-05 09:33:59 +01:00
Jukka Lehtosalo fe4acabdd7 Allow bytes in addition to str in google.protobuf.text_format (#5267) 2021-04-30 15:46:29 +02:00
Jukka Lehtosalo aff821c538 Improve requests.adapters.HttpAdapter.__init__ (#5266)
The `max_retries` argument can be `None`. It's handled here as `retries`:

https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/retry.py#L235
2021-04-30 14:15:13 +01:00
Jukka Lehtosalo 8bd3e16eef Add back module-level __getattr__ to encodings (#5261)
This is needed for submodules, such as `encodings.cp437`.
2021-04-28 07:45:15 -07:00
Jukka Lehtosalo 9134f7bc3c Fix type of typed_ast.ast27.Str.s (#4703)
The original type was too narrow.
2020-10-25 01:39:51 +02:00
Jukka Lehtosalo 5be9c91518 freezegun: Fix __exit__ and remove union return types (#4491)
These caused false positives, for example in code like this:

```
    with freeze_time(None) as ft:  # False positive here
        ft.tick(3)  # False positive here
```
2020-08-28 16:50:05 +01:00
Jukka Lehtosalo 3e966524b7 Add back six.moves.cStringIO (Python 3) (#4490)
This was accidentally removed in #4287.
2020-08-28 16:49:56 +01:00
Jukka Lehtosalo ab0f5519a9 Add back six.moves.range (Python 3) (#4481)
This was accidentally removed in #4287.
2020-08-25 16:42:15 +01: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
Jukka Lehtosalo 199b262f63 Add back six.moves.urllib.parse.unquote (Python 2) (#4414)
This was accidentally removed in #4287.
2020-08-07 17:56:54 +01:00
Jukka Lehtosalo 57a1a3937a Add back six.StringIO (Python 2) (#4413)
This was accidentally removed in #4287.
2020-08-07 17:41:48 +01:00
Jukka LehtosaloandJelle Zijlstra 7c444365f2 Add back six.moves.range (Python 2) (#4411)
This was accidentally removed in #4287.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2020-08-07 17:20:34 +01:00
Jukka Lehtosalo 29c71ff52a Add back tornado stubs for Python 2 only (#4412)
Tornado ships with inline annotations, but it no longer is Python 2
compatible. I think that it makes sense to keep the legacy stubs for
Python 2 compatibility, at least for a while longer.

This restores stubs removed in #4321, but moves them to
`third_party/2`.
2020-08-07 08:54:57 -07:00
Jukka Lehtosalo 5162c536c8 Make attributes of inspect.ArgSpec optional in Python 3 (#3838)
This makes them consistent with Python 2 stubs.

The attributes are documented here:
https://docs.python.org/3/library/inspect.html#inspect.getargspec
2020-03-10 13:15:20 +01:00
Jukka Lehtosalo 1c08bf7fa2 Improve an annotation in tornado stubs (#3809) 2020-03-05 14:34:14 +00:00
Jukka Lehtosalo 86135edb6d Fix signature of assertRaisesRegexp in unittest (#3434)
Fixes mypy false positive `"None" has noattribute "__enter__"` here:

```
class Foo(unittest.TestCase):
    def test_foo(self) -> None:
        with self.assertRaisesRegexp(Exception, "foo"):
            1 / 0
```

Fixes regression introduced in e6c467af82.
2019-11-01 15:48:35 +00:00
Jukka Lehtosalo eca93753ee Update the signature of decorator.decorator (#3336)
Nothing prevents a decorator defined using `@decorator` from
changing the signature of the decorated function. For example,
this example changes the return type to `str`:

```
from decorator import decorator

@decorator
def stringify(f, *args, **kwargs) -> str:
    return str(f(*args, **kwargs))
```

The old signature caused false positives in internal Dropbox code.

I couldn't come up with a signature that would produce better types
with mypy while not generating false positives.
2019-10-10 16:45:06 +01:00
Jukka Lehtosalo 3a2fdfd45d Make it explicit that the cryptography stubs are incomplete (#3331) 2019-10-09 16:20:36 +01:00
Jukka Lehtosalo 0914d50b49 Fix the signature of decorator.contextmanager (#3330) 2019-10-09 16:58:37 +02:00
Jukka Lehtosalo e3773ad691 Fix typos in yaml stubs (save_* -> safe_*) (#2985) 2019-05-13 11:57:34 -04:00
Jukka Lehtosalo 306b4694ae Remove redundant definition of ast.PyCF_ONLY_AST (#2803)
This gets imported from `_ast`, so the definition was
redundant. This causes problems with the new mypy semantic
analyzer, as it will flag the redefinition as an error.
2019-02-20 17:12:11 +00:00
Jukka Lehtosalo 184148611a Fix signature of TypedDict has_key() (#2672) 2018-12-05 18:17:37 +01:00
Jukka Lehtosalo c8890b0f93 Add mypy fallback class for TypedDict methods to mypy_extensions (#2670)
This class is not defined at runtime but it's used by
mypy internally to support TypedDict methods.

Use NoReturn in argument types for better type safety
when the related mypy plugin hook is not active.
2018-12-04 18:21:58 +00:00
Jukka Lehtosalo eaeb5fcf13 Revert "Move Sized earlier in the bases of Sequence (#2602)" (#2657)
This reverts commit 4dc21f04dd.

Fixes #2655.
2018-11-30 14:48:55 +00:00
Jukka Lehtosalo 84548f5bba Revert "fix list concatenation (#2404)" (#2653)
The fix caused regressions for mypy that are difficult to
fix.  See https://github.com/python/mypy/issues/5492 for
context.

This reverts commit 1a42a2c3ea.
2018-11-29 12:20:29 -08:00
Jukka Lehtosalo 07374a8251 Fix requests.post signature (#2256)
The change introduced in 395ab5abd1
broke the signature of `requests.post`, among others, since
`MutableSequence` is invariant, and plain `Dict[str, str]` values
were no longer valid for the `data` argument.

This changes the signature to have `Any` components as a
compromise. Adding more items to the union seems a bit too much, since
the error messages for invalid argument types are already pretty hard to
read.

Another option might be to use `Mapping` instead of `MutableMapping`
due to covariance, but I assume there's a reason why `MutableMapping`
is used here.
2018-06-19 17:08:55 +01: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
Jukka Lehtosalo a88d942f91 Add missing protobuf container methods (#2254)
Implementation of the methods is here:

https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/containers.py
2018-06-19 15:52:54 +01:00
Jukka Lehtosalo 9b612c9218 Add TypedDict total argument (#1443)
See https://github.com/python/mypy/pull/3558 for context.
2017-06-29 08:38:13 -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
Jukka Lehtosalo f2579e532f Update signature of dict.get (#852)
Without this change, mypy can't infer proper types for cases like
`d.get(k, [])` where it needs type context to infer the type of
`[]`. We add the value type to the second argument to `get` using
union types, and this provides the context. This doesn't affect
the effective signature of `get`, other than providing the type
context for mypy.

Also removed some related redundant method definitions where we can
just inherit the base class definition. This makes it easier to
keep the method signatures consistent.

Note that this requires a few mypy PRs before mypy will be able to
use this effectively:

* https://github.com/python/mypy/pull/2718
* https://github.com/python/mypy/pull/2715
2017-01-20 07:41:54 -08:00
Jukka Lehtosalo c577c84a17 Add stub for cryptography.hazmat.primitives.serialization (#851)
This makes the stubs for cryptoraphy less incompelete. Created using
stubgen. I don't know what the types are.
2017-01-20 07:40:30 -08:00
Jukka Lehtosalo 35978a7ca5 Make signature of DictMixin.get consistent with Mapping.get (#838) 2017-01-17 14:08:49 +00:00
Jukka Lehtosalo 734ad44a11 Make MRO of UserDict.DictMixin consistent with Mapping (#837)
This fixes MRO conflicts produced by mypy when using multiple
inheritance.
2017-01-17 13:55:31 +00:00
Jukka Lehtosalo f39f9bf694 Make object_hook of json.load and json.loads optional (Python 2) (#757) 2016-12-07 13:32:56 -08:00
Jukka Lehtosalo e05edb60f1 Mypy stub fixes for strict optional mode (#366) 2016-07-13 09:38:40 -07:00
Jukka Lehtosalo b98580d299 Don't use basestring in tempfile stub (#205)
Fixes #202.
2016-05-24 16:45:48 -07:00
Jukka Lehtosalo d3d6a47302 Accept more unicode in 2.7 tempfile stubs (#201) 2016-05-16 13:37:15 +01:00
Jukka Lehtosalo 292447bd62 Misc stub fixes (#181) 2016-05-04 16:54:57 -07:00
Jukka Lehtosalo a5d5dcc4f4 Fixes to os.environ (#172)
Add os.environ.copy() to Python 2 stubs. Fix return
type of os.environ.copy().
2016-04-28 11:53:51 +01:00
Jukka Lehtosalo 8df19841bb Make Queue generic in Python 2, similar to Python 3 (#155) 2016-04-22 15:49:02 +01:00
Jukka Lehtosalo 5eaf522cde Fixes to urllib2 stubs (#154) 2016-04-22 13:19:37 +01:00
Jukka Lehtosalo fcef262a9b Support keyword arguments for dict() (Python 2) (#148) 2016-04-17 13:07:54 +01:00
Jukka Lehtosalo 70c2274e94 Update dict(...) to accept keyword arguments (#147)
We can't describe the fact that a keyword argument results in a 'str'
dictionary key in a stub. This needs to be handled elsewhere.
2016-04-16 16:30:15 +01:00
Jukka Lehtosalo bd36a37481 Add __setattr__ to object 2015-12-05 15:44:28 -08:00
Jukka Lehtosalo 4c547fc4f6 Add object.__new__
Not entirely sure about the right signature, though.
2015-11-29 12:18:42 -08:00
Jukka Lehtosalo d3efa5d6bc Add missing definitions to Python 3 'traceback' 2015-11-23 21:49:51 -08:00
Jukka Lehtosalo 9640fe569d Add directories for Python 2 and Python 3 stubs 2015-03-04 20:57:49 -08:00
Jukka Lehtosalo 893f49bd80 Initial commit 2015-03-04 20:51:28 -08:00