-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Predict enum value type for unknown member names. #9443
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
Changes from 1 commit
fa67a31
077d3e7
e861117
db16871
bfee76d
b3653d7
f1adaf5
22c1474
aae9de3
d392d05
050fdc2
5fa5a93
5b8baa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
It is very common for enums to have homogenous member-value types. In the case where we do not know what enum member we are dealing with, we should sniff for that case and still collapse to a known type if that assuption holds.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,22 @@ class SomeEnum: | |
""" | ||
enum_field_name = _extract_underlying_field_name(ctx.type) | ||
if enum_field_name is None: | ||
# We do not know the ennum field name (perhaps it was passed to a function and we only | ||
# know that it _is_ a member). All is not lost however, if we can prove that the all | ||
# of the enum members have the same value-type, then it doesn't matter which member | ||
# was passed in. The value-type is still known. | ||
if isinstance(ctx.type, Instance): | ||
info = ctx.type.type | ||
gvanrossum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stnodes = (info.get(name) for name in info.names) | ||
first_node = next(stnodes, None) | ||
if first_node is None: | ||
return ctx.default_attr_type | ||
first_node_type = first_node.type | ||
if all(node is not None and node.type == first_node_type for node in stnodes): | ||
underlying_type = get_proper_type(first_node_type) | ||
if underlying_type is not None: | ||
return underlying_type | ||
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. If I read the docstring of 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 took a stab at this, but I'm not completely sure what you're saying here :). It's unclear to me whether the result of 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. Hm, I think I misunderstood this. I thought that using
but it doesn't seem to work that way. So you're forgiven for not understanding me. :-) I think what you have now is fine. |
||
|
||
return ctx.default_attr_type | ||
|
||
assert isinstance(ctx.type, Instance) | ||
gvanrossum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,26 @@ reveal_type(Truth.true.name) # N: Revealed type is 'Literal['true']?' | |
reveal_type(Truth.false.value) # N: Revealed type is 'builtins.bool' | ||
[builtins fixtures/bool.pyi] | ||
|
||
[case testEnumValueExtended] | ||
from enum import Enum | ||
class Truth(Enum): | ||
true = True | ||
false = False | ||
|
||
def infer_truth(truth: Truth) -> None: | ||
reveal_type(truth.value) # N: Revealed type is 'builtins.bool' | ||
[builtins fixtures/bool.pyi] | ||
|
||
[case testEnumValueInhomogenous] | ||
from enum import Enum | ||
class Truth(Enum): | ||
true = 'True' | ||
false = 0 | ||
|
||
def cannot_infer_truth(truth: Truth) -> None: | ||
reveal_type(truth.value) # N: Revealed type is 'Any' | ||
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 wonder if this shouldn't infer In favor of In favor of 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 was trying to just follow the existing art. I imagine that changing to With that said ... once this is implemented, it shouldn't matter to me either way. Hopefully 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. Okay, let's stick with tradition and keep |
||
[builtins fixtures/bool.pyi] | ||
|
||
[case testEnumUnique] | ||
import enum | ||
@enum.unique | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.