Make type function return Type[T]#1787
Conversation
This commit is a fix for python#1758 -- it special-cases the single constructor `type` builtin function within mypy so it returns an instance of `typing.Type` rather then just the generic `type` object.
mypy/checkexpr.py
Outdated
| formal_to_actual, context, | ||
| messages=arg_messages) | ||
|
|
||
| ret_val_is_type_obj = is_equivalent(callee.ret_type, self.named_type('builtins.type')) |
There was a problem hiding this comment.
That's a pretty expensive call, and this case rarely triggers, so the time is usually wasted. I suggest testing the simpler conditions first and doing this call last.
mypy/checkexpr.py
Outdated
| formal_to_actual, context, | ||
| messages=arg_messages) | ||
|
|
||
| if callee.is_type_obj() and (len(arg_types) == 1) and \ |
There was a problem hiding this comment.
The PEP 8 conformant way to break up such a line is
if (<long condition> and
<another long condition> and
<you get the point>):
<body>
Note that you have to indent the continuations an extra 4 spaces or you'll run afoul of some other PEP 8 recommendation.
(Following some debate, it's also okay to put the and at the start of the line -- mypy is inconsistent about this anyways, so do what you think is best or what you see nearby. :-)
|
Whee! |
|
There's a regression here. I've got an example: This gives: |
|
A simpler test case for the regression: type(3) == intThis causes the following errors: After some probing, this issue seems to be a manifestation of a pre-existing bug in mypy. The following program throws the exact same errors when I tried running it against both the current version of mypy and a version before the merge: int == intSwapping out class G: pass
G == G...results in: The problem seems to be with the |
This commit introduces a workaround to fix the bug discussed in python#1787 Previously, code where you compared two types (eg `int == int`) would cause mypy to incorrectly report a "too few arguments" error.
This commit introduces a workaround to fix the bug discussed in python#1787 Previously, code where you compared two types (eg `int == int`) would cause mypy to incorrectly report a "too few arguments" error.
This commit introduces a workaround to fix the bug discussed in #1787 Previously, code where you compared two types (eg `int == int` or `int != int`) would cause mypy to incorrectly report a "too few arguments" error.
This commit is a fix for #1758 -- it special-cases the single constructor
typebuiltin function within mypy so it returns an instance oftyping.Typerather then just the generictypeobject.It adds a special case within mypy rather then modifying the definition within Typeshed, but since
typeis defined to be a class and not a function definition in Typeshed, it was unclear how I could implement this change by only modifying Typeshed.This commit doesn't handle the
x.__class__attribute -- it still returnstype.