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

gh-123777: Allow __orig_class__ to be accessed in __init__ #123878

Closed
wants to merge 4 commits into from

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Sep 9, 2024

Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated
@@ -1290,13 +1290,30 @@ def __call__(self, *args, **kwargs):
if not self._inst:
raise TypeError(f"Type {self._name} cannot be instantiated; "
f"use {self.__origin__.__name__}() instead")
result = self.__origin__(*args, **kwargs)
clas = self.__origin__
is_python_type = clas.__new__ is type.__new__
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem to do what you want it to do:

>>> class A: pass
... 
>>> A.__new__ is type.__new__
False

This PR doesn't include tests; to get it merged we'll definitely need tests.

I'm also not convinced we should change this at all, since it feels like a risky change at this point.

Copy link
Member

Choose a reason for hiding this comment

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

You may want to compare against object.__new__ instead, but that still doesn't directly check for "Python types", and in any case I'm not sure it's a good idea to branch on whether it's a Python type.

>>> from typing import TypeVar
>>> TypeVar.__new__
<built-in method __new__ of type object at 0x14c817e30>
>>> A.__new__ is object.__new__
True

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, Alex had the same concern, I just pushed a better fix instead of type.__new__.

I'm also not convinced we should change this at all, since it feels like a risky change at this point.

Very much agreeing there, but there does seem to be some demand from the typing users. If this causes a regression somewhere, we could always roll back the changes!

Copy link
Member

Choose a reason for hiding this comment

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

If this causes a regression somewhere, we could always roll back the changes!

If we release this in 3.14.0, then backward compatibility will again mean we're stuck with the change. CPython is deep enough in the stack that we can't afford to just go back and forth.

Copy link
Member Author

Choose a reason for hiding this comment

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

You may want to compare against object.__new__ instead, but that still doesn't directly check for "Python types", and in any case I'm not sure it's a good idea to branch on whether it's a Python type.

>>> from typing import TypeVar
>>> TypeVar.__new__
<built-in method __new__ of type object at 0x14c817e30>
>>> A.__new__ is object.__new__
True

Oh yeah, that sounds like the right approach, my bad!

If we release this in 3.14.0, then backward compatibility will again mean we're stuck with the change. CPython is deep enough in the stack that we can't afford to just go back and forth.

Hmm, maybe we should bring this to Discourse.

Copy link
Member

Choose a reason for hiding this comment

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

Very much agreeing there, but there does seem to be some demand from the typing users.

Just because there's a problem doesn't mean that this is the best fix. __orig_class__ is completely undocumented and the last time we discussed potentially documenting it we decided not to because it sometimes has a counterintuitive value. For other typing-specific dunders our approach has generally been to expose public introspection helpers so that we're easily able to refactor internals later without breaking any public interface (e.g. get_protocol_members(), types.get_original_bases, etc.).

Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
@ZeroIntensity ZeroIntensity marked this pull request as draft September 9, 2024 20:34
@ZeroIntensity
Copy link
Member Author

I've converted this to a draft while I address adding tests and whatnot.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

I am -1 on this feature. It would add a rather big piece of complexity to our hot code path. It is rather hard to get right all the corner cases here.

Lib/typing.py Outdated
try:
result.__orig_class__ = self
# Some objects raise TypeError (or something even more exotic)
# if you try to set attributes on them; we guard against that here
except Exception:
pass

if is_python_type:
Copy link
Member

Choose a reason for hiding this comment

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

There can be C types with __init__ as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

As in, there are C types that might want to access their own __orig_class__?

@ZeroIntensity
Copy link
Member Author

As far as I can see, we aren't testing __orig_class__ anywhere! That's not good.

@AlexWaygood
Copy link
Member

As far as I can see, we aren't testing __orig_class__ anywhere! That's not good.

Because it's a private, undocumented implementation detail. See my comment at #123878 (comment).

@ZeroIntensity
Copy link
Member Author

Nevermind, it seems that GitHub search was being weird. We do have tests for __orig_class__ in the typing tests.

@ZeroIntensity
Copy link
Member Author

ZeroIntensity commented Sep 9, 2024

OK, I've put a test down. As I said in my comment, I'm not even convinced that this is a worthwhile feature.

@XieJiSS, implementing this seems like it will be problematic, as suspected. Is there something here that cannot be worked around at all with the current API? If not, I'm going to close this PR.

@XieJiSS
Copy link

XieJiSS commented Sep 11, 2024

Hi @ZeroIntensity, I'm currently using a workaround which basically enumerate all ancestor frames until it finds a frame whose f_locals contains a self that satisfies self.__origin__ is cls. I'm just not quite sure if this workaround works under all scenarios tho, e.g. am I actually using some internal mechanism of CPython's implementation that might change in the future?

@ZeroIntensity
Copy link
Member Author

__orig_class__ itself is an implementation detail that might change in the future. Accessing the previous self seems like a decent workaround to me. It could change, but it's probably not going to as long as __orig_class__ is a thing.

@ZeroIntensity
Copy link
Member Author

I'm going to close this; there seems to be a lot of pushback for something that is relatively simple to work around right now. I'm also worried that even with the __new__ check, this will cause problems with C types. (For example, when testing this, I came across some issues with _GenericAlias not liking it's __new__ to get called manually.)

@XieJiSS, if you (or others) would really like this, it's probably best to discuss it on discourse first. You could use this PR as a reference implementation, if you want a POC.

Any core dev or triager should close the original issue as not planned. Thanks!

@ZeroIntensity ZeroIntensity deleted the fix-orig-class-init branch September 12, 2024 00:49
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.

5 participants