-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Do not report higher-kinded types as Anys with --disallow-any=expr #3667
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
Conversation
as well as in `--any-exprs-report` and `--html-report` We can't fully represent higher-kinded types (e.g. calls to NewType) in mypy's type system, so we treat them as 'Any' and give specific errors for each higher-kinded type (e.g. NewType). Another design I considered is either returning object type or having a new HigherKindedType type. The problem with that approach is that because now a call to NewType() does not have type 'Any' if something is wrong with the call, it reports redundant errors. Consider the following situation: ``` C = NewType('C', int) C = NewType('C', str) ``` If `NewType(...)` has type object, in addition to all the expected errors, it will also report `Incompatible types in assignment` error.
I am not sure this is the best solution but I've been thinking about it and couldn't come up with anything better. |
As a random thought, I think |
Hmm I'm not sure that such approximations would be generally useful -- they may just be confusing. |
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.
Looks mostly fine, but I'm not excited about the naming.
mypy/types.py
Outdated
# Is this type a higher-kinded type (e.g. type of call expr to NewType or NamedTuple)? | ||
# We can't fully represent higher-kinded types in mypy's type system, so we treat them | ||
# as 'Any' and give specific errors for each higher-kinded type (e.g. NewType). | ||
self.is_higher_kinded_type = is_higher_kinded_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.
I'd rename this to something more general, since we don't really take advantage of the fact that these represent type constructors anywhere, and the concept of "higher-kinded type" sounds kind of complicated. My suggestion would be special_form
, which can be applied for things other than type constructors that don't have meaningful types but that also aren't imprecisely typed.
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.
Yeah, that does make sense. Updated!
I'm confused by the PR description. What is the problem you are trying to
solve, and what is your solution? The answers to these questions aren't so
clear from the current text.
|
Hmm, wow, yeah, I missed a big part of explanation. Thanks for pointing it out! Higher-kinded types (e.g. type of call to NewType) don't fit in mypy's type system, so we represent them as The solution proposed in this PR is to tag these Anys as "special form" and then don't report such Anys in |
I still don't understand -- in what sense doesn't NewType() fit in the type
system? Where does it get turned into Any? When I do
reveal_type(NewType('C', int)) I see Type[int], essentially, which seems
fine and not at all Any.
|
I think he means something like just |
No, I do mean type of call to
line with the Now, if we change |
OOC I just checked and it is already defined in typeshed that way. So I didn't invent anything new here :-)
Maybe I am missing something, but IIRC it is not. The |
@ilevkivskyi that is an interesting suggestion. If that works, it might better than what's currently in the PR. My concern is that we can miss some Anys if we don't look in Moreover, for |
@ilinum Let's keep the implementation as is (with the renaming that I suggested above), at least if you want to get it merged before release. We can refine this afterwards, but the idea to use callables introduces additional complexity which I'd rather not deal with this late in the release process. |
LGTM! |
Do not report higher-kinded types as Anys with --disallow-any=expr as well as in
--any-exprs-report
and--html-report
We can't fully represent higher-kinded types (e.g. calls to NewType) in mypy's type system,
so we treat them as 'Any' and give specific errors for each higher-kinded type (e.g. NewType).
Another design I considered is either returning object type or having a new HigherKindedType type.
The problem with that approach is that because now a call to NewType() does not have type 'Any'
if something is wrong with the call, it reports redundant errors.
Consider the following situation:
If
NewType(...)
has type object, in addition to all the expected errors,it will also report
Incompatible types in assignment
error.