Skip to content
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

Refactor Union, Tuple, and Callable #308

Merged
merged 39 commits into from
Oct 29, 2016
Merged

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Oct 25, 2016

Fixes #301 now List[Tuple[T, T]][int] == List[Tuple[int, int]]
Fixes #298 now Table = Tuple[T, List[T]] can be used as generic type alias (as in PEP 484 example)
Fixes #299 now class MyTup(Tuple[int, ...]): ... is allowed
Fixes #156 (well, it does not substitute the annotations, but makes this task simple even in complex cases, see example in tests)

Also this PR fixes some minor things that I have found while working on this:

  • List[Union] (with bare Union, i.e. without arguments), List[Optional], List[Generic[T]], and List[ClassVar[int]] are not valid and are prohibited now.
  • Generic did not evaluate forward references when asked, now it does.
  • __qualname__ was not copied on generic class subscription.
  • Type was not erased on instantiation of subclasses of concrete containers (List, Set, etc).
  • There was an obscure bug in Python 2: sometimes _abc_registry was erased on instantiation.

The main idea of this PR is to fix the issues mentioned at the top by reusing the existing code. Namely, I pulled flattening and removing duplicates code from _Union and the tree calculation function _subs_tree from GenericMeta. As well I moved Tuple and Callable after GenericMeta and made them inherit the latter. So that now all types that could be generic store their info in common way using __origin__, __parameters__, __args__.

I tried to polish this, to be sure that nothing was broken in the process of "refactoring" (also to improve speed). There is no recursion, the substitution tree is recalculated only when necessary. Also I added a lot of tests and many comments/docstrings (also for things added in my recent PRs).

@gvanrossum Could you please take a look at this? I would like this to get into 3.6b3, so that it will have a chance to be thoroughly tested.

@ilevkivskyi
Copy link
Member Author

@gvanrossum
OK, I have copied this to Python 2 and updated the PR description accordingly. Please take a look.

@ilevkivskyi
Copy link
Member Author

@gvanrossum Is there a chance this PR gets into 3.6b3?

(If this one gets merged, I could make a PR for #306 within an hour (if I will not sleep at that time :-))

@gvanrossum
Copy link
Member

gvanrossum commented Oct 28, 2016 via email

@gvanrossum
Copy link
Member

I'm not sure if I'll ever be able to actually review this. It's over 1000
lines. The only way this will ever land is on the basis of trust alone...

@ilevkivskyi
Copy link
Member Author

@gvanrossum Changes to Python 2 and 3 are practically identical, so this is rather 500 lines (even less if one does not count tests). But in no case I want you to feel pressure because of this PR :-)

The only way this will ever land is on the basis of trust alone...

I would say this is an argument in favour of including this PR in 3.6b3 (it will be more tested by wider community before 3.6 final).

Also note that this PR will enable more features from python/mypy#2378 , namely generic aliases like TInt = Tuple[T, int].

@gvanrossum
Copy link
Member

OK, here's what I'll do, right now. I'll merge this right now, sight unseen, so you can fix #306.

I will create a CPython issue representing typing.py upgrades, and mention that when I merge this into CPython.

Hopefully the buildbots will stay green, but if not, we'll find out on that issue.

When you have #306 fixed I will merge that into CPython too, referencing the same issue.

I will also try to decide on the parameters of Coroutine.

@gvanrossum gvanrossum merged commit 50d2b4d into python:master Oct 29, 2016
@gvanrossum
Copy link
Member

OK, those changes were pushed, now let's watch the buildbots: https://www.python.org/dev/buildbot/

@ilevkivskyi
Copy link
Member Author

Thank you for merging this! It looks like all buildbots are OK :-)

pavel-kirienko added a commit to OpenCyphal/pydsdl that referenced this pull request May 8, 2019
…Typing library, which is fixed in python/typing#308. The fixed typing library ships with Python 3.5.3 and newer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants