Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
50707ba
Implement typing.get_orig_class and get_orig_bases
Gobot1234 Feb 11, 2023
4fef9f8
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 11, 2023
4cb193d
Apparently my lsp wasn't working
Gobot1234 Feb 11, 2023
44d3ae9
Removing trailing whitespace
Gobot1234 Feb 11, 2023
59e9ac2
Fix test failure
Gobot1234 Feb 11, 2023
20024e9
Merge branch 'main' into orig_class-and-bases
Gobot1234 Feb 11, 2023
769fdbd
Fix typo
Gobot1234 Feb 11, 2023
7194d3e
Respond to initial review
Gobot1234 Feb 12, 2023
eeb4475
Remove trailing ws
Gobot1234 Feb 17, 2023
198dc08
Respond to second round of review
Gobot1234 Feb 25, 2023
695cf93
Fix typo
Gobot1234 Feb 25, 2023
cc11033
Fix last few comments
Gobot1234 Apr 5, 2023
a66282b
Merge remote-tracking branch 'upstream/main' into orig_class-and-bases
Gobot1234 Apr 5, 2023
d3768ba
Merge branch 'main' into orig_class-and-bases
Gobot1234 Apr 5, 2023
581a91a
Apply suggestions from code review
Gobot1234 Apr 6, 2023
fb15427
Remove typing.get_orig_class
Gobot1234 Apr 6, 2023
96b9536
Merge remote-tracking branch 'origin/orig_class-and-bases' into orig_…
Gobot1234 Apr 6, 2023
6c38e4d
Remove old test
Gobot1234 Apr 6, 2023
372bd6b
Rename to get_original_bases
Gobot1234 Apr 6, 2023
a6fe8ac
Autoformatting is fun
Gobot1234 Apr 6, 2023
2b71b74
Fix more suggestions
Gobot1234 Apr 6, 2023
e7635c6
Remove rawstring
Gobot1234 Apr 6, 2023
42e1668
Apply suggestions from code review
Gobot1234 Apr 7, 2023
1040478
Fix `test_types` failures
AlexWaygood Apr 8, 2023
8085258
Apply suggestions from code review
Gobot1234 Apr 8, 2023
b9bf4fd
Add a whatsnew entry
Gobot1234 Apr 8, 2023
ee59cbd
Update Lib/types.py
Gobot1234 Apr 8, 2023
e5a91a5
Update Doc/library/types.rst
Gobot1234 Apr 8, 2023
7bad429
Merge branch 'main' into orig_class-and-bases
AlexWaygood Apr 8, 2023
2a39055
Merge branch 'main' into orig_class-and-bases
Gobot1234 Apr 9, 2023
1ea82be
Remove trailing WS
Gobot1234 Apr 9, 2023
1cb14c2
Fix more trailing WS
Gobot1234 Apr 9, 2023
6f06c52
Fallback to __bases__
Gobot1234 Apr 9, 2023
689267a
Update missing docs
Gobot1234 Apr 9, 2023
1ae16ea
Apply suggestions from code review
Gobot1234 Apr 9, 2023
3a8619a
Update Doc/library/types.rst
Gobot1234 Apr 9, 2023
e1d55d6
Merge remote-tracking branch 'upstream/main' into orig_class-and-bases
AlexWaygood Apr 11, 2023
bab8cb3
Small tweaks to docs
AlexWaygood Apr 11, 2023
fb9ef70
Use `assert` in docs examples
AlexWaygood Apr 12, 2023
dc433c4
Document type.__orig_bases__
Gobot1234 Apr 12, 2023
4268b74
Revert "Document type.__orig_bases__"
Gobot1234 Apr 12, 2023
9f54ac1
Update Doc/library/types.rst
Gobot1234 Apr 19, 2023
c122b23
Hone docs more
AlexWaygood Apr 22, 2023
2134053
Merge remote-tracking branch 'upstream/main' into orig_class-and-bases
AlexWaygood Apr 23, 2023
cb21a65
More tests and docs following #103698
AlexWaygood Apr 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Respond to initial review
  • Loading branch information
Gobot1234 committed Feb 12, 2023
commit 7194d3e2b53ead8ee23c811aa5db1d28772241e0
7 changes: 6 additions & 1 deletion Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,8 @@ Introspection helpers
get_orig_bases(Bar) == (Foo[int], float)
get_orig_bases(int) == None
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated

.. versionadded:: 3.12

.. function:: get_orig_class(tp, /)
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated

Returns the ``GenericAlias`` object that was instantiated to create ``tp``.
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
Expand All @@ -2838,7 +2840,10 @@ Introspection helpers

.. warning::

This function will not work inside of the class initalisation process.
This function will always return ``None`` inside of the class initalisation
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
process.

.. versionadded:: 3.12

.. function:: is_typeddict(tp)

Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5358,18 +5358,27 @@ class C(Generic[T]): pass
def test_get_orig_class(self):
T = TypeVar('T')
class C(Generic[T]): pass
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
class D(C[int]): ...
class E(C[str], float): pass
self.assertEqual(get_orig_class(C[int]()), C[int])
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
self.assertIsNone(get_orig_class(C()))
self.assertIsNone(get_orig_class(D()))
self.assertIsNone(get_orig_class(E()))
self.assertIsNone(get_orig_class(list[int]()))
self.assertIsNone(get_orig_class(int))

def test_get_orig_bases(self):
T = TypeVar('T')
class A: pass
class B(object): pass
class C(Generic[T]): pass
Comment thread
Gobot1234 marked this conversation as resolved.
Outdated
class D(C[int]): pass
class E(C[str], float): pass
self.assertIsNone(get_orig_bases(A))
self.assertIsNone(get_orig_bases(B))
self.assertEqual(get_orig_bases(C), (Generic[T],))
self.assertEqual(get_orig_bases(D), (C[int],))
self.assertEqual(get_orig_bases(int), None)
self.assertIsNone(get_orig_bases(int))
self.assertEqual(get_orig_bases(E), (C[str], float))


Expand Down
3 changes: 2 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,8 @@ class Foo(Generic[T]): ...

Warning
-------
This will always None in the __init__/__new__ methods of the class.
This will always return None in the inside of the class initalisation
process.
"""
try:
return tp.__orig_class__
Expand Down