Skip to content

gh-102433: Add tests for how classes with properties interact with isinstance() checks on typing.runtime_checkable protocols #102449

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

Merged
merged 6 commits into from
Mar 11, 2023
Merged
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
Next Next commit
Address review
  • Loading branch information
AlexWaygood committed Mar 10, 2023
commit f2bc5403fbb34871fda6f9d7cd3dcf1de84dffe6
12 changes: 3 additions & 9 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2553,13 +2553,7 @@ class D:
class E(C): ...
class F(D): ...

for klass in C, D, E, F:
with self.subTest(klass=klass.__name__):
self.assertEqual(klass().attr, 42)

class G: ...

self.assertFalse(hasattr(G(), "attr"))
class Empty: ...

T = TypeVar('T')

Expand Down Expand Up @@ -2590,7 +2584,7 @@ class PG1(Protocol[T]):
self.assertIsInstance(klass(), protocol_class)

with self.subTest(protocol_class=protocol_class.__name__):
self.assertNotIsInstance(G(), protocol_class)
self.assertNotIsInstance(Empty(), protocol_class)

class BadP(Protocol):
@property
Expand All @@ -2607,7 +2601,7 @@ class BadPG1(Protocol[T]):
attr: T

for obj in PG[T], PG[C], PG1[T], PG1[C], BadP, BadP1, BadPG, BadPG1:
Copy link
Member

Choose a reason for hiding this comment

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

nit: these are all protocol classes still; the name 'protocol_class used in the above "good" loop seems much clearer than the name obj

Copy link
Member Author

@AlexWaygood AlexWaygood Mar 11, 2023

Choose a reason for hiding this comment

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

No strong opinion here, but the reason I avoided "protocol_class" for these ones is that the parameterised ones (PG[T], etc) aren't strictly speaking classes anymore — they're generic aliases to protocol classes.

for klass in C, D, E, F, G:
for klass in C, D, E, F, Empty:
with self.subTest(klass=klass.__name__, obj=obj):
with self.assertRaises(TypeError):
isinstance(klass(), obj)
Expand Down