Skip to content

Support type aliases in builtins - #5338

Merged
ilevkivskyi merged 1 commit into
python:masterfrom
ilevkivskyi:fix-bytes-alias
Jul 10, 2018
Merged

ilevkivskyi merged 1 commit into
python:masterfrom
ilevkivskyi:fix-bytes-alias

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

This fixes a recently discovered internal crash. This should probably go into the release, since this is a regression.

@ilevkivskyi
ilevkivskyi requested a review from msullivan July 9, 2018 22:23

@msullivan msullivan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good, with a question about a future refactor possibility:

How many places do we need to do this sort of check? If it is a lot, we might want a function that we call to collapse out TypeAliases for this sort of thing.

Comment thread mypy/checker.py
sym = self.lookup_qualified(name)
node = sym.node
if isinstance(node, TypeAlias):
assert isinstance(node.target, Instance)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It can't ever be a chain of TypeAliases?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It can't, chained aliases are collapsed on sight. An alias can target other types (non-Instance) but this function is supposed to be used only with built-in types, that are all instances.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

How many places do we need to do this sort of check? If it is a lot, we might want a function that we call to collapse out TypeAliases for this sort of thing.

This is actually a quite common pattern. I think I have seen at least three or four other occurrences of the same pattern. I will try to factor them all out.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

How many places do we need to do this sort of check? If it is a lot, we might want a function that we call to collapse out TypeAliases for this sort of thing.

This is actually a quite common pattern. I think I have seen at least three or four other occurrences of the same pattern. I will try to factor them all out.

I have found 11 places where a similar pattern is used, but the problem is that all of them are not like others (mostly we just want to detect unsubscripted aliases in different contexts, but don't rebind the node). I tried to play with it, but it looks like defining such a function will actually complicate the code.

I will then merge this one now, maybe we can revisit this refactoring later.

@ilevkivskyi
ilevkivskyi merged commit e6692ca into python:master Jul 10, 2018
@ilevkivskyi
ilevkivskyi deleted the fix-bytes-alias branch July 10, 2018 17:53
ilevkivskyi added a commit that referenced this pull request Jul 11, 2018
This fixes a recently discovered internal crash.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants