8000 Fix crash due to checking type variable values too early by JukkaL · Pull Request #4384 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix crash due to checking type variable values too early #4384

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 3 commits into from
Jan 2, 2018

Conversation

JukkaL
Copy link
Collaborator
@JukkaL JukkaL commented Dec 18, 2017

Move type variable checks which use subtype and type sameness
checks to happen at the end of semantic analysis.

The implementation also adds the concept of priorities to
semantic analysis patch callbacks. Callback calls are
sorted by the priority. We resolve forward references and
calculate fallbacks before checking type variable values,
as otherwise the latter could see incomplete types and crash.

Fixes #4200
Fixes #3977

Move type variable checks which use subtype and type sameness
checks to happen at the end of semantic analysis.

The implementation also adds the concept of priorities to
semantic analysis patch callbacks. Callback calls are
sorted by the priority. We resolve forward references and
calculate fallbacks before checking type variable values,
as otherwise the latter could see incomplete types and crash.

Fixes #4200.
Copy link
Member
@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks! I have just few optional comments.

# (after pass 3):

# Fix forward references (needs to happen first)
PRIORITY_FORWARD_REF = 0
Copy link
Member

Choose a reason for hiding this comment

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

Just a minor suggestion: I would use plural PRIORITY_FORWARD_REFS to be more consistent with other constants.

Copy link
Member

Choose a reason for hiding this comment

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

Why not just make this an enum? That should be possible now that mypy no longer supports being run on 3.3.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I decided to continue using the old idiom, since I like consistency and I don't want to change all the existing constants into enums :-)

or tvar.upper_bound.type.fullname() != 'builtins.object'):
# Some restrictions on type variable. These can only be checked later
# after we have final MROs and forward references have been resolved.
self.indicator['typevar'] = True
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated idea: indicator is a good candidate to be a TypedDict (but it is not in typing yet).

Sorry, something went wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, or maybe just a regular object.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, or maybe just a regular object.

:-) Good point!

mypy/typeanal.py Outdated
@@ -1036,3 +996,58 @@ def make_optional_type(t: Type) -> Type:
return UnionType(items + [NoneTyp()], t.line, t.column)
else:
return UnionType([t, NoneTyp()], t.line, t.column)


class TypeVariableChecker(TypeTranslator):
Copy link
Member
@ilevkivskyi ilevkivskyi Dec 18, 2017

Choose a reason for hiding this comment

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

I am wondering if this class should be in semanal_pass3.py.

@mitar
Copy link
mitar commented Dec 27, 2017

I was testing this branch and noticed an issue with where is recursive types error claimed to come from. I can observe the same in reproduction I made for #4385. I am not sure if this is because of this pull request, but this pull request make it even possible to observe the issue, so I am reporting it here.

So if you ran my reproduction, mypy bar, you get the following error:

foo/bary.py:5: error: Recursive types not fully supported yet, nested types replaced with "Any"
foo/bary.py:9: error: Recursive types not fully supported yet, nested types replaced with "Any"

If you look carefully, you can see that the line numbers are OK, but the file reported is wrong. It should be bar/types.py. This is where recursive types are defined. In foo/bary.py they are only used. (And only used once, so if error would be about use of the recursive type and not definition, there should be only one error, not two.)

This is problematic because it is hard to silence these errors. The only way I found was to use mypy.ini and silence the whole module. And even in that case I had to silence the wrong module, foo.bary and not bar.types.

@ilevkivskyi
Copy link
Member

@mitar Hm, that is strange, have you checked if #4396 fixes your problem? (I think it is likely unrelated to this PR.) If no, could you please open a separate issue?

@mitar
Copy link
mitar commented Dec 29, 2017

I merged this and branch from #4396, but I see no difference in output. I will open an issue, #4413.

@JukkaL JukkaL merged commit b99f43f into master Jan 2, 2018
@gvanrossum gvanrossum deleted the fix-forward-ref branch January 2, 2018 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants
0