question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

False positive for accessing enum "instance fields"

See original GitHub issue

For 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nipunn1313commented, Mar 3, 2019

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.

0reactions
nipunn1313commented, Jun 10, 2019

I believe #79 should cover

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found