Skip to content

Conversation

lindnemi
Copy link
Contributor

At the moment the solve_network function can not be imported from other scripts, because the snakemake object required by extra_functionality is a global variable.

This PR defines the extra_functionality within solve_network, and adds the snakemake object as an additional argument to solve_network, s.t. the snakemake object can be captured from the scope of solve_network instead of the global scope.
This allows imports of the solve_network function from other scripts.

For an application see: PyPSA/pypsa-de#103

Before completing the PR i'd like to hear some opinions @fneum @lkstrp

Closes # (if applicable).

Changes proposed in this Pull Request

Checklist

  • I tested my contribution locally and it works as intended.
  • Code and workflow changes are sufficiently documented.
  • Changed dependencies are added to envs/environment.yaml.
  • Changes in configuration options are added in config/config.default.yaml.
  • Changes in configuration options are documented in doc/configtables/*.csv.
  • Sources of newly added data are documented in doc/data_sources.rst.
  • A release note doc/release_notes.rst is added.

@lindnemi lindnemi added the DNMY Do not merge yet label Jul 31, 2025
Copy link
Contributor

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

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

since moving this will likely lead to a lot of merge conflicts in downstream forks, what you can do alternatively is to pass the snakemake object to n._snakemake and change

        custom_extra_functionality(n, snapshots, snakemake)  # pylint: disable=E0601

to

        custom_extra_functionality(n, snapshots, n._snakemake)

@coroa
Copy link
Member

coroa commented Jul 31, 2025

Or you make an explicit argument to extra_functionality:

def extra_functionality(
    n: pypsa.Network, snapshots: pd.DatetimeIndex, planning_horizons: str | None = None, snakemake: Snakemake
) -> None:

and then pass it to n.optimize with:

n.optimize(..., extra_functionality=partial(extra_functionality, snakemake=snakemake))

@lindnemi
Copy link
Contributor Author

I like the partial suggestion

While we are at it: If snakemake is passed to solve_network should we get rid of all the other keywords as well?

@lindnemi lindnemi requested a review from FabianHofmann August 15, 2025 14:58
@lindnemi lindnemi removed the DNMY Do not merge yet label Aug 15, 2025
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.

3 participants