-
-
Notifications
You must be signed in to change notification settings - Fork 122
Add @solid_base
(PEP 800)
#634
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
base: main
Are you sure you want to change the base?
Changes from all commits
8b51be6
7b00a6a
f75df29
7b6b3a3
808a0d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ | |
'reveal_type', | ||
'runtime', | ||
'runtime_checkable', | ||
'solid_base', | ||
'Text', | ||
'TypeAlias', | ||
'TypeAliasType', | ||
|
@@ -315,6 +316,33 @@ class Other(Leaf): # Error reported by type checker | |
return f | ||
|
||
|
||
if hasattr(typing, "solid_base"): # 3.15 | ||
solid_base = typing.solid_base | ||
else: | ||
def solid_base(cls): | ||
"""This decorator marks a class a solid base. | ||
|
||
Child classes of a solid base cannot inherit from other solid bases that are | ||
not parent classes of the solid base. | ||
|
||
For example: | ||
|
||
@solid_base | ||
class Solid1: pass | ||
|
||
@solid_base | ||
class Solid2: pass | ||
|
||
class Solid3(Solid1, Solid2): pass # Type checker error | ||
|
||
Type checkers can use knowledge of solid bases to detect unreachable code | ||
and determine when two types can overlap. | ||
|
||
See PEP 800.""" | ||
cls.__solid_base__ = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it not be better to protect this with a Most solid bases from the standard library will not permit you to set this attribute: >>> int.__solid_base__ = True
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
int.__solid_base__ = True
^^^^^^^^^^^^^^^^^^
TypeError: cannot set '__solid_base__' attribute of immutable type 'int' And you might plausibly write a metaclass to make class objects immutable even if a class is written in pure Python -- I don't think you should have to avoid adding this decorator (which exists primarily to help out static type checkers) just because the class is immutable at runtime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it that way in Not necessarily opposed to adding the try-except though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, it just seems sort-of odd to set it unconditionally, given that most pre-existing solid bases without |
||
return cls | ||
|
||
|
||
def IntVar(name): | ||
return typing.TypeVar(name) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.