-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Handle types.ModuleType #3107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle types.ModuleType #3107
Changes from 2 commits
585ff8d
fbcb6ff
7c739df
b53c904
def95ce
6e8c81a
c43d345
ef70f3e
b956d46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,17 +153,18 @@ def f(c:str) -> None: pass | |
| [case testInvalidOperationsOnModules] | ||
| import m | ||
| import typing | ||
|
|
||
| class A: pass | ||
| m() # E: "module" not callable | ||
| a = m # type: A # E: Incompatible types in assignment (expression has type "module", variable has type "A") | ||
| m + None # E: Unsupported left operand type for + ("module") | ||
| m() # E: "ModuleType" not callable | ||
| a = m # type: A # E: Incompatible types in assignment (expression has type "ModuleType", variable has type "A") | ||
| m + None # E: Unsupported left operand type for + ("ModuleType") | ||
| [file m.py] | ||
| [builtins fixtures/module.pyi] | ||
|
|
||
| [case testNameDefinedInDifferentModule] | ||
| import m, n | ||
| import typing | ||
| m.x # E: "module" has no attribute "x" | ||
| m.x # E: "ModuleType" has no attribute "x" | ||
| [file m.py] | ||
| y = object() | ||
| [file n.py] | ||
|
|
@@ -329,7 +330,7 @@ import nonexistent | |
| [out] | ||
| tmp/x.py:1: error: Cannot find module named 'nonexistent' | ||
| tmp/x.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help) | ||
| main:3: error: "module" has no attribute "z" | ||
| main:3: error: "ModuleType" has no attribute "z" | ||
|
|
||
| [case testUnknownModuleImportedWithinFunction] | ||
| def f(): | ||
|
|
@@ -647,7 +648,7 @@ def f(x: str) -> None: pass | |
| if object(): | ||
| import m | ||
| else: | ||
| m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "module") | ||
| m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "ModuleType") | ||
| [file m.py] | ||
| [builtins fixtures/module.pyi] | ||
| [out] | ||
|
|
@@ -751,7 +752,7 @@ value = 3.2 | |
| [case testSubmoduleImportFromDoesNotAddParents] | ||
| from a import b | ||
| reveal_type(b.value) # E: Revealed type is 'builtins.str' | ||
| b.c.value # E: "module" has no attribute "c" | ||
| b.c.value # E: "ModuleType" has no attribute "c" | ||
| a.value # E: Name 'a' is not defined | ||
|
|
||
| [file a/__init__.py] | ||
|
|
@@ -852,7 +853,7 @@ bar = parent.unrelated.ShouldNotLoad() | |
| [builtins fixtures/module.pyi] | ||
| [out] | ||
| tmp/parent/child.py:8: error: Revealed type is 'parent.common.SomeClass' | ||
| tmp/parent/child.py:9: error: "module" has no attribute "unrelated" | ||
| tmp/parent/child.py:9: error: "ModuleType" has no attribute "unrelated" | ||
|
|
||
| [case testSubmoduleMixingImportFromAndImport2] | ||
| import parent.child | ||
|
|
@@ -1406,3 +1407,12 @@ reveal_type(cb) # E: Revealed type is 'def (*Any, **Any) -> Any' | |
| from typing import Callable, Any | ||
| AnyCallable = Callable[..., Any] | ||
| [out] | ||
|
|
||
| [case testRevealType] | ||
| import types | ||
| def f() -> types.ModuleType: | ||
| return types | ||
| reveal_type(f()) # E: Revealed type is 'types.ModuleType' | ||
| reveal_type(types) # E: Revealed type is 'types.ModuleType' | ||
|
|
||
| [builtins fixtures/module.pyi] | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| import typing | ||
|
|
||
| T = typing.TypeVar('T') | ||
| class list(typing.Generic[T], typing.Sequence[T]): pass | ||
|
|
||
| class object: | ||
| def __init__(self): pass | ||
| class type: pass | ||
| class function: pass | ||
| class int: pass | ||
| class str: pass | ||
| class dict: pass | ||
| class list: pass | ||
| class set: pass | ||
| class tuple: pass | ||
| class BaseException: pass | ||
| class StopIteration(BaseException): pass | ||
| class StopAsyncIteration(BaseException): pass | ||
| def iter(obj: typing.Any) -> typing.Any: pass | ||
| def next(obj: typing.Any) -> typing.Any: pass | ||
| class ellipsis: ... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,19 @@ | ||
| from typing import Any, Dict, Generic, TypeVar | ||
| from typing import Any, Dict, Generic, TypeVar, Sequence | ||
| from types import ModuleType | ||
|
|
||
| T = TypeVar('T') | ||
| S = TypeVar('S') | ||
|
|
||
| class list(Generic[T], Sequence[T]): pass | ||
|
|
||
| class object: | ||
| def __init__(self) -> None: pass | ||
| class module: | ||
| __name__ = ... # type: str | ||
| __file__ = ... # type: str | ||
| __dict__ = ... # type: Dict[str, Any] | ||
| class type: pass | ||
| class function: pass | ||
| class int: pass | ||
| class str: pass | ||
| class bool: pass | ||
| class tuple: pass | ||
| class dict(Generic[T, S]): pass | ||
| class ellipsis: pass | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| from typing import Generic, Sequence, TypeVar | ||
| from types import ModuleType | ||
|
|
||
| _T = TypeVar('_T') | ||
|
|
||
| class object: | ||
| def __init__(self) -> None: pass | ||
| class module: pass | ||
| class type: pass | ||
| class function: pass | ||
| class int: pass | ||
| class str: pass | ||
| class bool: pass | ||
| class list(Generic[_T], Sequence[_T]): | ||
| def append(self, x: _T): pass | ||
| def extend(self, x: Sequence[_T]): pass | ||
| def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass | ||
| class tuple: pass | ||
| class ellipsis: pass |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,34 @@ | ||
| from typing import TypeVar | ||
| from typing import TypeVar, Optional, List, Any, Generic, Sequence | ||
| T = TypeVar('T') | ||
|
|
||
| def coroutine(func: T) -> T: | ||
| return func | ||
|
|
||
| class bool: ... | ||
|
|
||
| class ModuleSpec: | ||
|
||
| def __init__(self, name: str, loader: Optional['Loader'], *, | ||
| origin: str = None, loader_state: Any = None, | ||
| is_package: bool = None) -> None: ... | ||
| name = ... # type: str | ||
| loader = ... # type: Optional[Loader] | ||
| origin = ... # type: Optional[str] | ||
| submodule_search_locations = ... # type: Optional[List[str]] | ||
| loader_state = ... # type: Any | ||
| cached = ... # type: Optional[str] | ||
| parent = ... # type: Optional[str] | ||
| has_location = ... # type: bool | ||
|
|
||
| class Loader: | ||
| def load_module(self, fullname: str) -> ModuleType: ... | ||
| def module_repr(self, module: ModuleType) -> str: ... | ||
| def create_module(self, spec: ModuleSpec) -> Optional[ModuleType]: ... | ||
| def exec_module(self, module: ModuleType) -> None: ... | ||
|
|
||
| class ModuleType: | ||
| __name__ = ... # type: str | ||
| __file__ = ... # type: str | ||
| __loader__ = ... # type: Optional[Loader] | ||
| __package__ = ... # type: Optional[str] | ||
| __spec__ = ... # type: Optional[ModuleSpec] | ||
| def __init__(self, name: str, doc: Optional[str] = ...) -> None: ... | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
types.pyi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just in time for the last commit =)