Skip to content

[dev] experimental: override variables of base#4

Merged
LoSealL merged 1 commit into
mainfrom
dev
Aug 7, 2025
Merged

[dev] experimental: override variables of base#4
LoSealL merged 1 commit into
mainfrom
dev

Conversation

@LoSealL

@LoSealL LoSealL commented Aug 6, 2025

Copy link
Copy Markdown

When using read_base() context, we can override variables in the base config file by:

with read_base(foo=1):
  from ..base import foo

Now foo in the base will be overriden to 1.

When using `read_base()` context, we can override variables
in the base config file by:
```python
with read_base(foo=1):
  from ..base import foo
```
Now `foo` in the base will be overriden to 1.
@LoSealL LoSealL merged commit 6225f68 into main Aug 7, 2025
9 checks passed
@LoSealL LoSealL deleted the dev branch August 7, 2025 06:22
@LoSealL LoSealL requested a review from Copilot August 7, 2025 06:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces experimental functionality to override variables in base configuration files when using the read_base() context manager. The implementation allows passing key-value pairs to read_base() that will override corresponding variables in the imported base modules.

Key changes:

  • Added support for keyword arguments in read_base() to specify variable overrides
  • Implemented AST transformation to apply overrides during config parsing
  • Updated test cases to validate the new override functionality

Reviewed Changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
mme/config/config.py Core implementation of override functionality in config parsing
mme/config/utils.py Added OverideAssignTransformer class for AST transformation
tests/test_config/test_base_modification.py Updated test to validate override behavior
tests/data/config/lazy_module_config/test_change_base_var.py Modified test config to use override syntax
tests/data/config/lazy_module_config/test_base.py Extended base config with additional variables
pyproject.toml Updated Python version support and dependencies
mme/init.py Version bump to 0.10.10
tests/test_config/test_lazy.py Removed deprecated numpy.compat imports
tests/test_config/test_config_legacy.py Simplified temporary directory test logic
tests/data/config/lazy_module_config/test_ast_transform.py Removed deprecated numpy.compat import
.github/workflows/test.yml Added codecov integration
.github/workflows/lint.yml Updated Python setup action and added pre-commit formatting
.github/pull_request_template.md Removed entire PR template content

Comment thread mme/config/utils.py
return node


class OverideAssignTransformer(ast.NodeTransformer):

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

Class name contains a typo: 'Overide' should be 'Override'.

Suggested change
class OverideAssignTransformer(ast.NodeTransformer):
class OverrideAssignTransformer(ast.NodeTransformer):

Copilot uses AI. Check for mistakes.
Comment thread mme/config/config.py
from .utils import (
ConfigParsingError,
ImportTransformer,
OverideAssignTransformer,

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

Import name contains a typo: 'OverideAssignTransformer' should be 'OverrideAssignTransformer'.

Suggested change
OverideAssignTransformer,
OverrideAssignTransformer,

Copilot uses AI. Check for mistakes.
Comment thread mme/config/config.py
modified_code, filename=filename
)
if override_vars:
transform2 = OverideAssignTransformer(override_vars)

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

Class instantiation contains a typo: 'OverideAssignTransformer' should be 'OverrideAssignTransformer'.

Suggested change
transform2 = OverideAssignTransformer(override_vars)
transform2 = OverrideAssignTransformer(override_vars)

Copilot uses AI. Check for mistakes.
Comment thread mme/config/utils.py
# must be SSA
if len(node.targets) != 1:
return node
if (key := node.targets[0].id) in self.override_vars:

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

This code assumes node.targets[0] has an id attribute, but it could be other types like ast.Subscript or ast.Attribute. This will raise an AttributeError for assignments like x[0] = 1 or obj.attr = 1.

Suggested change
if (key := node.targets[0].id) in self.override_vars:
if isinstance(node.targets[0], ast.Name) and (key := node.targets[0].id) in self.override_vars:

Copilot uses AI. Check for mistakes.
Comment thread mme/config/config.py
except Exception as e:
raise SyntaxError(
except ImportError:
warnings.warn("formater 'black' is not found.")

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

Typo in warning message: 'formater' should be 'formatter'.

Suggested change
warnings.warn("formater 'black' is not found.")
warnings.warn("formatter 'black' is not found.")

Copilot uses AI. Check for mistakes.
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

Successfully merging this pull request may close these issues.

2 participants