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

TOML set_env file support #3478

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

juditnovak
Copy link

@juditnovak juditnovak commented Feb 5, 2025

This code change is addressing #3474, making sure that TOML config has the same support to apply set_env from file, as INI configuration does.


def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
if conf is None:
replaced = raw_ # no replacement supported in the core section
Copy link
Author

Choose a reason for hiding this comment

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

TBH this condition I've just taken from the INI correspondant... Pls double-check if it makes sense in TOML parsing context. Thanks.

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 make sense here. Please revert changes to this.

@juditnovak juditnovak force-pushed the fix/toml_setenv_from_file branch 2 times, most recently from 10c8e9c to 7dc1f78 Compare February 5, 2025 01:18
@juditnovak juditnovak marked this pull request as ready for review February 5, 2025 09:03
@juditnovak juditnovak changed the title [WIP] [FIX] TOML set_env from file [FIX] TOML set_env from file Feb 5, 2025
("conf_type", "config"),
[
("ini", "[testenv]\npackage=skip\nset_env=file|A{/}a.txt\nchange_dir=C"),
("toml", '[env_run_base]\npackage="skip"\nset_env="file|A{/}a.txt"\nchange_dir="C"'),
Copy link
Member

Choose a reason for hiding this comment

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

It would make more sense that this to be a dictionary key rather than we just unroll a string. Something like 'set_env = [ { file= "a.txt"}] `.

Copy link
Author

Choose a reason for hiding this comment

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

Just to make sure that I'm following correctly. Could you pls confirm that the following is what you suggest?

INI:

The current syntax supported for tox.ini for set_env:

[testenv]
set_env=file|<path>

TOML:

Moving away from the file| syntax introduced for tox.ini, benefitting from embedded data structures instead such as:

[env_run_base]
set_env=[{file=<path>}]

While this would raise an ERROR in TOML:

[env_run_base]
set_env="file|<path>"

Is this the direction you would like to take? Not to support file| terminology when TOML configuration is used?

(Sorry, I understood that a PR was welcome for the issue as raised -- which my attempt was aiming to respond. Of course, I'm glad to modify the code alongside a different design, if preferred. I agree that the above solution is more elegant.)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, with the TOML format we generally are trying to move away from embedding information into strings, and instead use the rich type system and structure of the TOML language instead.

@gaborbernat
Copy link
Member

You still plan to tackle this?

@gaborbernat gaborbernat marked this pull request as draft February 10, 2025 19:06
@juditnovak
Copy link
Author

@gaborbernat Yes I do. Sorry for the silence, I was unavailable for a couple of days. Updated PR should be available early next week.

@juditnovak juditnovak force-pushed the fix/toml_setenv_from_file branch from 6fdde68 to 81a6997 Compare February 18, 2025 10:59
@gaborbernat
Copy link
Member

@juditnovak tag me when ready 👍

@juditnovak
Copy link
Author

@gaborbernat Sorry for the delay, the PR is ready. I keep thinking that I'm kind unhappy with the solution in terms of separation of concerns -- as part of the INI/TOML processing is happening on the level of each loader (__init__.py), while format-specific logic now is also hitting the shared (ideally generic) set_env module...
(Yet, otherwise set_env logic would need to "cascade up" to each loader's level :-/ )

In case I'm overlooking a different, more elegant solution, feel free to let me know, and I'm glad to adjust.

@juditnovak juditnovak marked this pull request as ready for review February 21, 2025 13:37
@gaborbernat gaborbernat changed the title [FIX] TOML set_env from file TOML set_env file support Feb 21, 2025

def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
if conf is None:
replaced = raw_ # no replacement supported in the core section
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 make sense here. Please revert changes to this.

@juditnovak juditnovak force-pushed the fix/toml_setenv_from_file branch from 04b9290 to e4cabe8 Compare February 21, 2025 17:41
@juditnovak
Copy link
Author

@gaborbernat The condition in question is removed (assuming that 'revert' meant removal of newly added code).

Comment on lines +68 to +85
delay_replace = inspect.isclass(of_type) and issubclass(of_type, SetEnv)

def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
reference_replacer = Unroll(conf, self, args)
try:
replaced = str(reference_replacer(raw_)) # do replacements
except Exception as exception:
if isinstance(exception, HandledError):
raise
msg = f"replace failed in {args_.env_name}.{key} with {exception!r}"
raise HandledError(msg) from exception
return replaced

exploded = Unroll(conf=conf, loader=self, args=args)(raw)
return self.to(exploded, of_type, factory)
refactoried = self.to(exploded, of_type, factory)
if delay_replace:
refactoried.use_replacer(replacer, args=args) # type: ignore[attr-defined] # issubclass(to_type, SetEnv)
return refactoried
Copy link
Member

Choose a reason for hiding this comment

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

I mean this file should not be changed at all 🤔

Copy link
Author

Choose a reason for hiding this comment

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

Could you please elaborate on your preferred strategy, please?

I was following the INI logic keeping INI and TOML processing side-by-side. If a different logic is preferred, may I ask what you have in mind?

Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this processing for TOML? This is an ini specific stuff 🤔

Copy link
Author

Choose a reason for hiding this comment

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

No, it's not. The replacer function is used within the set_env module, to determine the correct path, that may have substitutions inside.

Note that we are using the TOML-specific Unroll() call within the code block in question.

This is the reason why TOML-specific tests with file="{env:myenvfile}work.

@gaborbernat gaborbernat self-assigned this Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants