Skip to content

Conversation

@7576457
Copy link
Contributor

@7576457 7576457 commented Oct 17, 2025

Description

As mentioned in the issue, handling environment variables is currently inconsistent.
This PR aims to address this by using Click for a more consistent solution:

  • Add a new --quiet-console flag to control LITESTAR_QUIET_CONSOLE.
  • Introduce envflag(), which returns True for 1, true, t, yes, on, y, and False otherwise.
  • Apply envflag() wherever CLI or other code reads environment variables representing flags.

Closes

Also add a new utility function:
- envflag(): parses environment variables as boolean

Applied envflag() to read some environment variables
@github-actions github-actions bot added area/private-api This PR involves changes to the privatized API size: small pr/external Triage Required 🏥 This requires triage labels Oct 17, 2025
@codecov
Copy link

codecov bot commented Oct 17, 2025

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.83%. Comparing base (94073d3) to head (bd3b530).

Files with missing lines Patch % Lines
litestar/utils/warnings.py 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4443   +/-   ##
=======================================
  Coverage   97.83%   97.83%           
=======================================
  Files         296      296           
  Lines       15295    15300    +5     
  Branches     1713     1714    +1     
=======================================
+ Hits        14964    14969    +5     
  Misses        189      189           
  Partials      142      142           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@7576457 7576457 changed the title Make environment flag parsing consistent with Click feat: make environment flag parsing consistent with click Oct 19, 2025
@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4443

@7576457 7576457 marked this pull request as ready for review October 19, 2025 17:40
@7576457 7576457 requested review from a team as code owners October 19, 2025 17:40

from enum import Enum
from functools import partial
from os import getenv
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from os import getenv
import os

return cast("type[BaseException]", _ExceptionGroup)


def envflag(varname: str, default: bool = False) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not behave like click. It will consider all values it doesn't consider True to be False, so e.g. a typo like Tru or 2 would also be considered False.

It should raise in that case instead. Please consult the click docs in the original issue I've linked on the actual behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I’ve addressed your comments.

According to the click documentation you linked: "Any other value is interpreted as deactivating the flag". This means invalid values like Tru or 2 should return False, not raise an error. Could you clarify what behavior you're expecting?

Comment on lines 124 to 126
envvar = getenv(varname)
if not envvar:
return default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will return default if the value is set to "", which is not what we want.

Comment on lines 54 to 55
assert envflag("TEST_FLAG") is True
del os.environ["TEST_FLAG"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the monkeypatch fixture for this instead.

https://docs.pytest.org/en/stable/how-to/monkeypatch.html

 * envflag: removed default parameter.
 * envflag: behavior now matches click.BoolParamType.
 * envflag: raises an error if none of the conditions are met.
 * tests(envflag): updated tests and applied monkeypatch.
@7576457
Copy link
Contributor Author

7576457 commented Oct 23, 2025

@provinzkraut
I’ve applied your feedback and made the changes. Please review them.
Also, I have a question regarding raising an error.

I’m currently using LitestarException. Would it be better to create a more specific error, for example LitestarEnvironmentError, in litestar/exceptions/base_exceptions.py?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/private-api This PR involves changes to the privatized API pr/external size: small Triage Required 🏥 This requires triage type/feat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants