Visitar URL original
bpo-44353: Refactor typing.NewType into callable class by uriyyo · Pull Request #27250 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220203020657/https://github.com/python/cpython/pull/27250
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

bpo-44353: Refactor typing.NewType into callable class #27250

Merged
merged 1 commit into from Jul 20, 2021

Conversation

@uriyyo
Copy link
Member

@uriyyo uriyyo commented Jul 19, 2021 •

https://bugs.python.org/issue44353

def __init__(self, name, tp):
self.__name__ = name
self.__qualname__ = name
self.__module__ = _callee(default='typing')
Copy link
Contributor

@ambv ambv Jul 20, 2021

Choose a reason for hiding this comment

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

That makes it rather slow but I guess __module__ is actually helpful.

Copy link
Member Author

@uriyyo uriyyo Jul 20, 2021

Choose a reason for hiding this comment

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

It will be executed only once when NewType defined. I think it should not dramatically effect performance.

@ambv ambv merged commit 965dd76 into python:main Jul 20, 2021
13 checks passed
@miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Jul 20, 2021

Thanks @uriyyo for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Jul 20, 2021

GH-27258 is a backport of this pull request to the 3.10 branch.

miss-islington added a commit to miss-islington/cpython that referenced this issue Jul 20, 2021
(cherry picked from commit 965dd76)

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
@Fidget-Spinner
Copy link
Contributor

@Fidget-Spinner Fidget-Spinner commented Jul 20, 2021

@ambv oh wow I wasn't expecting this to get merged yet, on bpo it was still being discussed https://bugs.python.org/issue44353#msg397842. I'm +0 for this personally, so if you're +1 I certainly won't complain.

self.assertEqual(UserId | int, Union[UserId, int])
self.assertEqual(int | UserId, Union[int, UserId])

self.assertEqual(get_args(UserId | int), (UserId, int))
self.assertEqual(get_args(int | UserId), (int, UserId))
Copy link
Contributor

@Fidget-Spinner Fidget-Spinner Jul 20, 2021 •

Choose a reason for hiding this comment

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

I think this doesn't test for the bug the user is reporting. In fact without this PR, these tests should all work, just instead of being typing.Union, it is types.Union (because int implements __or__). So the test won't be able to detect a regression.

The bug reported is that UserId1 | UserId2 won't work, because they both don't implement __or__.

Copy link
Member Author

@uriyyo uriyyo Jul 20, 2021

Choose a reason for hiding this comment

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

You are right, we should add tests to cover case when there are two NewType.

Copy link
Member Author

@uriyyo uriyyo Jul 20, 2021

Choose a reason for hiding this comment

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

I will open another PR with more tests. I was not expecting that it will be merged so fast)

@uriyyo
Copy link
Member Author

@uriyyo uriyyo commented Jul 23, 2021

@Fidget-Spinner I notice that NewType become pickable. Should we add tests to cover this?

@ambv
Copy link
Contributor

@ambv ambv commented Jul 23, 2021

@uriyyo Good point! If you add a PR with such tests, I'll merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants