-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make type function return Type[T] #1787
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.
@@ -250,6 +250,11 @@ def check_call(self, callee: Type, args: List[Node], | |||
self.check_argument_types(arg_types, arg_kinds, callee, | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -250,6 +250,11 @@ def check_call(self, callee: Type, args: List[Node], | |||
self.check_argument_types(arg_types, arg_kinds, callee, | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) == int This 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 == int Swapping 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
type
builtin function within mypy so it returns an instance oftyping.Type
rather then just the generictype
object.It adds a special case within mypy rather then modifying the definition within Typeshed, but since
type
is 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
.