False positive for accessing enum "instance fields"
See original GitHub issueFor example, let’s assume a Money
message (containing amount and currency) and a Money.Currency
enum.
This should be valid:
some_money = Money()
some_money.amount = 200
some_money.currency = Money.USD
and
some_money.currency = Money.Currency.Value('CAD')
However, this should be invalid (but is currently valid):
cad = some_money.currency.Value('CAD')
Similarly, this is a false positive:
Money.Currency('CAD')
since it finds int.__init__
although in runtime it would result in:
TypeError: 'EnumTypeWrapper' object is not callable
One way to solve it would be introduce a CurrencyValue
:
class Money(google___protobuf___message___Message):
CurrencyValue = NewType('int')
class Currency:
DESCRIPTOR: ...
...
USD = typing___cast(CurrencyValue, 1)
...
This would be a breaking change since it’ll require changing any signature accepting Money.Currency
. Also, since it exists only in the type stubs, you’d have to quote it, i.e.
def do_something(currency: 'Money.CurrencyValue'):
Alternatively, there might be some (new?) mypy construct that allows to designate fields and functions as class-access only – basically the same as its built-in (hardcoded, from what I could tell) support for enum
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
False positive for accessing enum "instance fields" #65 - GitHub
mypy construct that allows to designate fields and functions as class-access only -- basically the same as its built-in (hardcoded, from what I ......
Read more >rust - Why can't I access enum fields directly? - Stack Overflow
It is because weight isn't part of the Animal enum, it's part of the Cat type of the enum. Say in the future...
Read more >Enum HOWTO — Python 3.11.1 documentation
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more...
Read more >Fundamentals of Java Enum Types - SitePoint
Java enum types make it easy to define a fixed number of constants. More than that, enums are full-blown classes and can have...
Read more >A tour of the Dart language
Similarly, Dart supports top-level variables, as well as variables tied to a class or object (static and instance variables). Instance variables are ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure I follow. I can try to play with it later to get a better understanding. Can you provide a full example (with contents of proto file, python callsite, expected mypy error, actual runtime result)
Would be great to have a testcase to really spell it out.
I believe #79 should cover