Giter VIP home page Giter VIP logo

strenum's People

Contributors

clbarnes avatar ericbn avatar erperreault avatar grimmer0125 avatar igoose1 avatar irgeek avatar treyhunner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

strenum's Issues

Proposal: using Poetry

How about using Poetry to manage dependencies? Also potentially could use tox to test against different python versions.

Mypy does not accept Literal StrEnum types

Under mypy 0.991, it doesn't appear that we can use Literal types whose values are StrEnum elements. With the following sample code:

from enum import Enum
from typing import Literal, TypedDict

from strenum import StrEnum


class Entree(str, Enum):
    spam = "SPAM"
    eggs = "EGGS"


class Side(StrEnum):
    bacon = "BACON"
    grits = "GRITS"


class Breakfast(TypedDict):
    entree: Entree
    side: Side


class EggBreakfast(TypedDict):
    entree: Literal[Entree.eggs]
    side: Side


class BaconBreakfast(TypedDict):
    entree: Entree
    side: Literal[Side.bacon]

The mypy output is:

type-error.py:28: error: Variable "type-error.Side.bacon" is not valid as a type  [valid-type]
type-error.py:28: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
type-error.py:28: error: Parameter 1 of Literal[...] is invalid  [valid-type]
Found 2 errors in 1 file (checked 1 source file)

Note that no error is thrown for Literal[Entree.eggs], which is defined on a (str, Enum) value rather than a StrEnum value.

I don't know whether this can be resolved from outside mypy, but I wanted to make the maintainers here aware if they're not, and find out if there's a better way to do this that I haven't come up with yet. In particular, Literal["BACON"] does remove the error, but is clearly not ideal.

Cleaner syntax suggestion

Some other libraries such as @dataclass, just use type hints without explicit value set

class C:
    a: int

Maybe we could do the same and use this:

class HTTP(StrEnum):
    GET: str
    POST: str

instead of (or in addition to) the current syntax:

class HTTP(StrEnum):
    GET = auto()
    POST = auto()

I'd argue that makes it look more pythonic by being explicit in the intent of the variables being of typestr

mypy error 'has incompatible type "auto"'

Hi,

I'm getting an error message when running mypy. Here's a reproducer

mypy.py

from enum import auto
from strenum import StrEnum

class HttpMethod(StrEnum):
    GET = auto()
    HEAD = auto()

class MyClass:
    def __init__(self, method: HttpMethod) -> None:
        self.method = method

MyClass(HttpMethod.GET)

mypy error message:

mypy --ignore-missing-imports mypy.py
mypy.py:16: error: Argument 1 to "MyClass" has incompatible type "auto"; expected "HttpMethod"
Found 1 error in 1 file (checked 1 source file)

When installing, StrEnum adds a `tests` package to site-packages

It seems that StrEnum installation creates a new package in site-packages.

ls <path-to-virtualenv>/site-packages/tests/
__init__.py  __pycache__  test_comparable.py  test_name_mangler.py  test_strenum_casefold.py  test_strenum_name_mangler.py  test_strenum.py

In my case, this caused tests in my project to fail, as importing something like tests.mock_data would no longer work.

I'm using Poetry, this may make matters worse in this case.

I believe this is unintended.

Making available on conda-forge

Hi,

Thank you for this package. I am cutting a PR to the conda-forge/staged-recipes repo to make this available via the Conda Forge for those who use anaconda as their package manager. Would you like to be listed as a recipe maintainer as well as me? Totally understand either way. Just as an FYI, if you do wish to be listed as a recipe maintainer, version updates are generally automated, but every now and then they require manual intervention. You will also periodically get notifications related to the repo associated with the recipe.

Let me know what you decide, and again, thanks!

Case-insensitive StrEnum

Is there any way to make case-insensitive StrEnum without changing EnumMeta class?

That is what I mean:

class InterpMethods(StrEnum, case_sensitive=False):
    LINEAR = auto()
    CUBIC = auto()
    LANCZOS = auto()

print(InterpMethods.LINEAR)
# <InterpMethods.LINEAR: 'LINEAR'>

assert InterpMethods.LINEAR == 'linear'
assert InterpMethods.LINEAR == 'Linear'
assert InterpMethods.LINEAR == 'LINEAR'

assert InterpMethods('linear') == InterpMethods.LINEAR
assert InterpMethods('Linear') == InterpMethods.LINEAR
assert InterpMethods('LINEAR') == InterpMethods.LINEAR

Real-life usage example:

import click
from strenum import StrEnum, auto
from something import linear, cubic, lanczos

class InterpMethods(StrEnum, case_sensitive=False):
    LINEAR = auto(), linear
    CUBIC = auto(), cubic
    LANCZOS = auto(), lanczos
    
    def __new__(cls, value, calculator):
        obj = str.__new__(cls, value)
        obj._value_ = value
        obj.apply = calculator
        return obj

@click.command()
@click.option('-m', '--method', type=click.Choices(InterpMethods, case_sensitive=False))
def cli(method: InterpMethods):
    ...
    result = method.apply(data)

# Usage of CLI:
# > cli -m linear
# > cli -m Linear
# > cli -m LINEAR

mypy is unable to find stub files

Steps to reproduce.

  1. Create new folder, initialize and activate a python3.7 virtual environment there.
mkdir check_strenum && cd !$
python3.7 -m venv env && . env/bin/activate
  1. Install strenum and mypy
pip install strenum mypy
  1. Create a test python script, name it check_strenum.py:
from strenum import StrEnum

class MyCheck(StrEnum):
    PASS: str = 'pass'
    FAIL: str = 'fail'
  1. Check code with mypy:
mypy --install-types --strict check_strenum.py

Actual result:

check_strenum.py:1: error: Skipping analyzing "strenum": module is installed, but missing library stubs or py.typed marker  [import]
check_strenum.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
check_strenum.py:3: error: Class cannot subclass "StrEnum" (has type "Any")  [misc]
Found 2 errors in 1 file (checked 1 source file)

Expected result:

Success: no issues found in 1 source file

Here are the contents of the package after it's installed with pip.

$ ll env/lib/python3.7/site-packages/strenum/
total 36K
drwxr-xr-x  3 eugenesqr eugenesqr 4.0K Apr 27 13:16 .
drwxr-xr-x 19 eugenesqr eugenesqr 4.0K Apr 27 13:16 ..
drwxr-xr-x  2 eugenesqr eugenesqr 4.0K Apr 27 13:16 __pycache__
-rw-r--r--  1 eugenesqr eugenesqr 8.4K Apr 27 13:16 __init__.py
-rw-r--r--  1 eugenesqr eugenesqr 2.0K Apr 27 13:16 mixins.py
-rw-r--r--  1 eugenesqr eugenesqr 3.4K Apr 27 13:16 _name_mangler.py
-rw-r--r--  1 eugenesqr eugenesqr  498 Apr 27 13:16 _version.py

Note that *.pyi files are missing.

If we manually copy stub files from the repo along with py.typed, it starts working:

cd env/lib/python3.7/site-packages/strenum/
touch py.typed
curl -O https://raw.githubusercontent.com/irgeek/StrEnum/master/strenum/__init__.pyi

I believe these files should be included into the result package. For example via setup.py:

package_data={'strenum': ['*.pyi', 'py.typed']},

Support for __contains__

I found that StrEnums couldn't support "in" so I wrote this for my own work:

class StrEnumMeta(EnumMeta):
    def __contains__(cls, item):
        if isinstance(item, str):
            if item in cls.__members__.values():
                return True
            else:
                return False
        try:
            return super().__contains__(item)
        except TypeError:
            return False

class MyEnum(str, Enum, metaclass=StrEnumMeta):
    ITEM_ONE = 'one'
    ITEM_TWO = 'two'

Might be worth making a metaclass for this...

Make dumping StrEnum works

Could you add this method to base StrEnum so that pydantic.BaseModel.model_dump and list when applied to StrEnum can return a normal string value?

    class StrEnum:
    ...
    def __repr__(self) -> str:
        return str.__repr__(self.value)

I am currently subclassing StrEnum to make it work, but couldn't be bothered to add it to every class you have here. Do you think this is a reasonable design choice for this lib?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.