Skip to content

Commit 58aef05

Browse files
authored
Include stubs for mypy_extensions in bundled typeshed (#10507)
Also don't install a stub package for `mypy_extensions`. There are two reasons why we're doing this: * We make assumptions about what's defined in `mypy_extensions`, and if users are free to modify it, various things may break, and this could even cause mypy crashes. * Older mypy versions complain about installed stubs for `mypy_extensions`. Updating to an earlier mypy versions in the same environment is broken if we use a stub package.
1 parent 7f2377e commit 58aef05

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

misc/sync-typeshed.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def update_typeshed(typeshed_dir: str, commit: Optional[str]) -> str:
4242
shutil.rmtree(stub_dir)
4343
# Copy new stdlib stubs.
4444
shutil.copytree(os.path.join(typeshed_dir, 'stdlib'), stub_dir)
45+
# Copy mypy_extensions stubs. We don't want to use a stub package, since it's
46+
# treated specially by mypy and we make assumptions about what's there.
47+
shutil.copy(os.path.join(typeshed_dir, 'stubs', 'mypy-extensions', 'mypy_extensions.pyi'),
48+
stub_dir)
4549
return commit
4650

4751

mypy-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
typing_extensions>=3.7.4
22
mypy_extensions>=0.4.3,<0.5.0
33
typed_ast>=1.4.0,<1.5.0
4-
types-mypy-extensions>=0.4.0
54
toml

mypy/stubinfo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
'markupsafe': 'types-MarkupSafe',
4848
'maxminddb': 'types-maxminddb',
4949
'mock': 'types-mock',
50-
'mypy_extensions': 'types-mypy-extensions',
5150
'OpenSSL': 'types-openssl-python',
5251
'orjson': 'types-orjson',
5352
'paramiko': 'types-paramiko',

mypy/test/teststubtest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,6 @@ def test_get_typeshed_stdlib_modules(self) -> None:
834834
assert "builtins" in stdlib
835835
assert "os" in stdlib
836836
assert "os.path" in stdlib
837-
assert "mypy_extensions" not in stdlib
838837
assert "asyncio" in stdlib
839838
assert ("dataclasses" in stdlib) == (sys.version_info >= (3, 7))
840839

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import abc
2+
import sys
3+
from typing import Any, Callable, Dict, Generic, ItemsView, KeysView, Mapping, Optional, Type, TypeVar, Union, ValuesView
4+
5+
_T = TypeVar("_T")
6+
_U = TypeVar("_U")
7+
8+
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
9+
class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
10+
def copy(self: _T) -> _T: ...
11+
# Using NoReturn so that only calls using mypy plugin hook that specialize the signature
12+
# can go through.
13+
def setdefault(self, k: NoReturn, default: object) -> object: ...
14+
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
15+
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # type: ignore
16+
def update(self: _T, __m: _T) -> None: ...
17+
if sys.version_info >= (3, 0):
18+
def items(self) -> ItemsView[str, object]: ...
19+
def keys(self) -> KeysView[str]: ...
20+
def values(self) -> ValuesView[object]: ...
21+
else:
22+
def has_key(self, k: str) -> bool: ...
23+
def viewitems(self) -> ItemsView[str, object]: ...
24+
def viewkeys(self) -> KeysView[str]: ...
25+
def viewvalues(self) -> ValuesView[object]: ...
26+
def __delitem__(self, k: NoReturn) -> None: ...
27+
28+
def TypedDict(typename: str, fields: Dict[str, Type[Any]], total: bool = ...) -> Type[Dict[str, Any]]: ...
29+
def Arg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
30+
def DefaultArg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
31+
def NamedArg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
32+
def DefaultNamedArg(type: _T = ..., name: Optional[str] = ...) -> _T: ...
33+
def VarArg(type: _T = ...) -> _T: ...
34+
def KwArg(type: _T = ...) -> _T: ...
35+
36+
# Return type that indicates a function does not return.
37+
# This type is equivalent to the None type, but the no-op Union is necessary to
38+
# distinguish the None type from the None value.
39+
NoReturn = Union[None] # Deprecated: Use typing.NoReturn instead.
40+
41+
# This is intended as a class decorator, but mypy rejects abstract classes
42+
# when a Type[_T] is expected, so we can't give it the type we want
43+
def trait(cls: Any) -> Any: ...
44+
def mypyc_attr(*attrs: str, **kwattrs: object) -> Callable[[_T], _T]: ...
45+
46+
class FlexibleAlias(Generic[_T, _U]): ...

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ def run(self):
194194
install_requires=["typed_ast >= 1.4.0, < 1.5.0; python_version<'3.8'",
195195
'typing_extensions>=3.7.4',
196196
'mypy_extensions >= 0.4.3, < 0.5.0',
197-
'types-mypy-extensions>=0.4.0',
198197
'toml',
199198
],
200199
# Same here.

0 commit comments

Comments
 (0)