8000 Type[C] by gvanrossum · Pull Request #1569 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Type[C] #1569

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

Merged
merged 23 commits into from
Jun 8, 2016
Merged

Type[C] #1569

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b7ddf36
Tentative tests for Type[C].
May 18, 2016
eb61f1d
Pave the way for implementing Type[C].
May 20, 2016
7b52ac6
WIP: Introducing TypeType: Type[x] becomes TypeType(x).
May 20, 2016
1fd14f0
WIP: Fix/new tests; implement more subtype checks.
May 23, 2016
371e425
WIP: Towards supporting Type[T].
May 23, 2016
96696ff
Fix things so all tests pass!
May 24, 2016
aa37595
Some cleanup and fixup (not done).
May 24, 2016
d4745b0
Improve equivalence between type and Type.
May 25, 2016
cf4a0b7
Add support for method lookup from Type[].
May 25, 2016
81ee239
Print a proper error for unsupported Type[] args.
May 26, 2016
3d92049
Reject Type[U] where U's bound is a generic class.
May 26, 2016
5ab11d2
Support classes with @overload-ed __init__.
May 26, 2016
afb0482
Update our copies of typing.py to the latest from the python/typing r…
May 26, 2016
db30d59
Make Type[A] erase to Type[A], but Type[T] erases to Type[Any].
Jun 6, 2016
6029c9d
Special-case joining Type[] with builtins.type.
Jun 7, 2016
786ef96
Special-case joining Type[] with builtins.type.
Jun 7, 2016
ef247f2
Delete outdated XXX comment.
Jun 7, 2016
4162103
Finishing touch -- fix and test TypeAnalyser.visit_type_type().
Jun 7, 2016
26ff267
Added tests as requested.
Jun 7, 2016
256b31a
Remove comment asking for name for analyze_type_type_callee().
Jun 7, 2016
d224aff
Use self.default(self.s) instead of AnyType()/NonType() in join/meet.
Jun 7, 2016
d9c74a0
Add tests for overloaded __init__.
Jun 7, 2016
eaa9e0d
Add more tests. Fixed a bug in join() this found.
Jun 7, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add more tests. Fixed a bug in join() this found.
  • Loading branch information
Guido van Rossum committed Jun 7, 2016
commit eaa9e0d74d4a12f36a62ff6ee9ffd05a7d7af492
6 changes: 4 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ def visit_type_type(self, left: TypeType) -> bool:
if isinstance(right, CallableType):
# This is unsound, we don't check the __init__ signature.
return right.is_type_obj() and is_subtype(left.item, right.ret_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we verify that the signature of __init__ of the left type is compatible with the callable on the right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we aren't checking the signature anywhere so it would be consistent to not have to check it here either. Maybe add a comment about the unsoundness, though.

if isinstance(right, Instance) and right.type.fullname() == 'builtins.type':
# Treat builtins.type the same as Type[Any].
if (isinstance(rig 8000 ht, Instance) and
right.type.fullname() in ('builtins.type', 'builtins.object')):
# Treat builtins.type the same as Type[Any];
# treat builtins.object the same as Any.
return True
return False

Expand Down
36 changes: 36 additions & 0 deletions mypy/test/data/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,10 @@ bar(Y)
from typing import Type, Any
def foo(arg: Type[Any]):
x = arg()
x = arg(0)
x = arg('', ())
reveal_type(x) # E: Revealed type is 'Any'
x.foo
class X: pass
foo(X)
[out]
Expand Down Expand Up @@ -1687,3 +1690,36 @@ from typing import TypeVar, Type
class B: pass
T = TypeVar('T', bound=Type[B])
def f(a: T): pass
[out]

[case testTypeUsingTypeCTuple]
from typing import Type, Tuple
def f(a: Type[Tuple[int, int]]):
a()
[out]
main: note: In function "f":
main:2: error: Unsupported type Type["Tuple[int, int]"]

[case testTypeUsingTypeCNamedTuple]
from typing import Type, NamedTuple
N = NamedTuple('N', [('x', int), ('y', int)])
def f(a: Type[N]):
a()
[builtins fixtures/list.py]
[out]
main: note: In function "f":
main:3: error: Unsupported type Type["N"]

[case testTypeUsingTypeCJoin]
from typing import Type
class B: pass
class C(B): pass
class D(B): pass
def foo(c: Type[C], d: Type[D]) -> None:
x = [c, d]
reveal_type(x)

[builtins fixtures/list.py]
[out]
main: note: In function "foo":
main:7: error: Revealed type is 'builtins.list[Type[__main__.B]]'
2 changes: 1 addition & 1 deletion mypy/test/data/fixtures/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class list(Iterable[T], Generic[T]):
@overload
def __init__(self) -> None: pass
@overload
def __init__(self, x: Iterator[T]) -> None: pass
def __init__(self, x: Iterable[T]) -> None: pass
def __iter__(self) -> Iterator[T]: pass
def __mul__(self, x: int) -> list[T]: pass
def __getitem__(self, x: int) -> T: pass
Expand Down
0