Visitar URL original
[3.5] bpo-28556: Update to typing: treat subscripted generics as proxies (#265) by Mariatta · Pull Request #269 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
bpo-28556: Fix regression that sneaked into recent typing updates (GH…
…-270)

(cherry picked from commit 365cb5b)
  • Loading branch information
ilevkivskyi authored and Mariatta committed Feb 24, 2017
commit 8128d0dfee176e45b816e38b9950206517d2d829
8 changes: 8 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,14 @@ class D(C[T]):
self.assertEqual(D.x, 'from derived x')
self.assertEqual(D[str].z, 'from derived z')

def test_abc_registry_kept(self):
T = TypeVar('T')
class C(Generic[T]): ...
C.register(int)
self.assertIsInstance(1, C)
C[int]
self.assertIsInstance(1, C)

def test_false_subclasses(self):
class MyMapping(MutableMapping[str, str]): pass
self.assertNotIsInstance({}, MyMapping)
Expand Down
5 changes: 4 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,10 @@ def __copy__(self):

def __setattr__(self, attr, value):
# We consider all the subscripted genrics as proxies for original class
if attr.startswith('__') and attr.endswith('__'):
if (
attr.startswith('__') and attr.endswith('__') or
attr.startswith('_abc_')
):
super(GenericMeta, self).__setattr__(attr, value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a small question, why not just using super().__setattr__(attr, value) but super(GenericMeta, self)? Isn't that equivalent at this case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In principle you are right. This is mostly for similarity with next case (also there is Python 2 backport of this module, so that sometimes I just write the same code for both)

else:
super(GenericMeta, _gorg(self)).__setattr__(attr, value)
Expand Down