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.

Using enum for clear API

See original GitHub issue

This proposal was originally borned in our discussion here.

Proposal (by @sobolevn):

Maybe we could use enums more often? It really covers this case easily. And it is possible to statically check your code with enum.

Here’s the sample code:

from enum import Enum

class PortRange(Enum):
      DEFAULT = (1, 65535)
      WELL_KNOWN = (1, 1023)
      EPHEMERAL = (49152, 65535)
      REGISTERED = (1024, 49151)

And then:

def port(self, range_: PortRange = PortRange.DEFAULT) -> int:

So, we want to discuss with community what is better? Let’s discuss.

If you like this idea, add 👍, otherwise 👎.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lk-geimfaricommented, Nov 16, 2017

@sobolevn It’s for returning random gender, maybe it’s good idea to rename it to RANDOM?

0reactions
sobolevncommented, Nov 18, 2017

I like the second one. That was my original point, when I have fired an issue.

I am not sure about RANDOM by the way. I have heard a lot of developers said that it does not belong there. That enum represent items. RANDOM is not an item.

Maybe it is not to late to replace it with a function?

 def mime_type(self, type_: Optional[MimeType] = None) -> str:
        if type_ is None:
              type_ = select_random_item(MimeType)
        ...
        return self.random.choice(MIME_TYPES[type_.value])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Enums & APIs - DEV Community ‍ ‍
Enums are a double-edged sword. They are extremely useful to create a set of possible values, but they can be a versioning problem...
Read more >
How Experienced Developers Use Enums in APIs
Let's say you remove the enum from the API response. You'll break the principle of taking nothing away and redefining things. · Let's...
Read more >
How to expose an enum to public API without ... - Stack Overflow
The original problem comes from I want to use Enums and Enums cannot extend your own class. With class hierarchy this would not...
Read more >
AIP-126: Enumerations - API Improvement Proposals
APIs may expose enum objects for sets of values that are expected ... An exception to this rule is if there is a...
Read more >
Naming conventions for enum values · Issue #530 - GitHub
There are no naming conventions that cover enum values. I've seen several different styles in our APIs so far: MY_VALUE; my-value; my_value.
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