Skip to content
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

Revert ignore_case deprecation from 13.0.0 #386

Closed
Mogost opened this issue Jan 9, 2025 · 10 comments
Closed

Revert ignore_case deprecation from 13.0.0 #386

Mogost opened this issue Jan 9, 2025 · 10 comments

Comments

@Mogost
Copy link

Mogost commented Jan 9, 2025

Version 13.0.0 was introduced Backwards-incompatible change

Remove ignore_case param from Env.enum.

This change makes usage less convenient. Additionally, environs only respects enum "keys", not values, which seems illogical in many cases because values often represent the actual data being used in comparisons or user inputs, while keys are more like internal identifiers.

from enum import StrEnum, auto
class Color(StrEnum):
    RED = auto()
print(Color.RED == "red")  # True
print(Color.RED)  # red
@sloria
Copy link
Owner

sloria commented Jan 9, 2025

It's not documented (yet), but you can pass by_value=True if you expect the envvar to be set as the value.

import os
from enum import StrEnum, auto

from environs import env

os.environ["COLOR"] = "red"


class Color(StrEnum):
    RED = auto()


COLOR = env.enum("COLOR", enum=Color, by_value=True)

assert isinstance(COLOR, Color)
assert COLOR == Color.RED
print(COLOR)  # => "red"

@Mogost does this meet your use case?

@Mogost
Copy link
Author

Mogost commented Jan 10, 2025

@sloria, this meets my case; thank you. Unfortunately, I cannot use it; mypy is not satisfied with that.

./settings/base.py:37: error: No overload variant of "__call__" of "EnumFieldMethod" matches argument types "str", "type[InstanceType]", "bool", "InstanceType"  [call-overload]
./settings/base.py:37: note: Possible overload variants:
./settings/base.py:37: note:     def __call__(self, name: str, *, enum: type[Any], validate: Callable[[Any], Any] | Iterable[Callable[[Any], Any]] | None = ..., required: bool = ...) -> Any
./settings/base.py:37: note:     def __call__(self, name: str, default: None = ..., *, enum: type[Any], validate: Callable[[Any], Any] | Iterable[Callable[[Any], Any]] | None = ..., required: bool = ...) -> Any | None
./settings/base.py:37: note:     def __call__(self, name: str, default: Any = ..., *, enum: type[Any], validate: Callable[[Any], Any] | Iterable[Callable[[Any], Any]] | None = ..., required: bool = ...) -> Any
INSTANCE_TYPE = env.enum("INSTANCE_TYPE", default=InstanceType.UNDEFINED, enum=InstanceType, by_value=True)

class InstanceType(enum.StrEnum):
    PROD = enum.auto()
    DEV = enum.auto()
    UNDEFINED = enum.auto()

@sloria
Copy link
Owner

sloria commented Jan 10, 2025

@Mogost ah thanks for reporting that. i've fixed the typing in #388. I'll release it in 14.1.0 today.

@Mogost
Copy link
Author

Mogost commented Jan 10, 2025

@sloria thank you!

Also one offtop question. Is this usage of the "default" argument correct in the new version? (especially ma.missing)

TENANT_ID = env.str(
    "TENANT_ID",
    default=default_tenant_id_by_instance_type.get(INSTANCE_TYPE) or ma.missing,
    validate=Regexp(r"^[a-zA-Z0-9_-]+$", error="TENANT_ID must contain only letters, numbers, underscores, and dashes"),
).lower()

@sloria
Copy link
Owner

sloria commented Jan 10, 2025

@Mogost you'll need to change ma.missing to ... or Ellipsis.

TENANT_ID = env.str(
    "TENANT_ID",
    default=default_tenant_id_by_instance_type.get(INSTANCE_TYPE) or Ellipsis,
    validate=Regexp(r"^[a-zA-Z0-9_-]+$", error="TENANT_ID must contain only letters, numbers, underscores, and dashes"),
).lower()

@sloria
Copy link
Owner

sloria commented Jan 10, 2025

14.1.0 is released with the by_value fix, so closing this

@sloria sloria closed this as completed Jan 10, 2025
@maximz
Copy link

maximz commented Jan 14, 2025

Hey @sloria , long time no chat 👋 Thanks for this awesome library.

Unfortunately the removal of ignore_case broke my usage. Unlike the OP, I can't switch to enum values, as my enum values are dataclasses. We will make it work with exact cased strings, but just wanted to put in a vote for a future return of ignore_case if it's on your roadmap. Thanks for considering this!

@sloria
Copy link
Owner

sloria commented Jan 14, 2025

oh hello @maximz 👋

i'm hesitant to add a special case in the codebase to resurrect ignore_case, and it's arguably better to enforce a single casing constraint on envvars anyway.

if your envvar casing differs from the enum name casing, you could do something like this

import os
from dataclasses import dataclass
from enum import Enum

from environs import env, validate

# Envvar uses lowercase names
os.environ["ANIMAL"] = "dog"

@dataclass
class AnimalConfig:
    genus: str

# Enum uses uppercase names
class Animal(Enum):
    DOG = AnimalConfig("Canis")
    CAT = AnimalConfig("Felis")


animal_config_name = env.str("ANIMAL", validate=validate.OneOf({v.name.lower() for v in Animal}))
animal_config = Animal[animal_config_name.upper()]

print(animal_config.value.genus)  # Canis

@maximz
Copy link

maximz commented Jan 14, 2025

@sloria Sounds good — thanks for the quick reply and suggested workaround! Love the new singleton env by the way. Hope all is well with you

@sloria
Copy link
Owner

sloria commented Jan 14, 2025

great! glad it works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants