-
Notifications
You must be signed in to change notification settings - Fork 61
adding types to utilities #252
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
Open
ParticularlyPythonicBS
wants to merge
1
commit into
unstable
Choose a base branch
from
typing/utilities
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,13 @@ | |
| periods, and regions. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import re | ||
| import sqlite3 | ||
| from os import PathLike | ||
| from typing import Any, cast | ||
|
|
||
| import deprecated | ||
| import pandas as pd | ||
|
|
@@ -55,9 +58,9 @@ def __init__(self, database_path: str | PathLike[str], scenario: str | None = No | |
|
|
||
| def close(self) -> None: | ||
| """Closes the database cursor and connection.""" | ||
| if self.cur: | ||
| if hasattr(self, 'cur') and self.cur: | ||
| self.cur.close() | ||
| if self.con: | ||
| if hasattr(self, 'con') and self.con: | ||
| self.con.close() | ||
|
|
||
| @staticmethod | ||
|
|
@@ -115,6 +118,8 @@ def get_time_peridos_for_flags(self, flags: list[str] | None = None) -> set[int] | |
| query = f'SELECT period FROM time_period WHERE flag IN ({in_clause})' | ||
|
|
||
| self.cur.execute(query) | ||
| # cast to int because sqlite might return strings or ints depending on how data was inserted | ||
| # but type hint says set[int] | ||
| return {int(row[0]) for row in self.cur} | ||
|
|
||
| def get_technologies_for_flags(self, flags: list[str] | None = None) -> set[str]: | ||
|
|
@@ -125,7 +130,7 @@ def get_technologies_for_flags(self, flags: list[str] | None = None) -> set[str] | |
| in_clause = ', '.join(f"'{flag}'" for flag in flags) | ||
| query = f'SELECT tech FROM Technology WHERE flag IN ({in_clause})' | ||
|
|
||
| return {row[0] for row in self.cur.execute(query)} | ||
| return {cast('str', row[0]) for row in self.cur.execute(query)} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial LGTM, but consider removing quotes around type. The 🤖 Prompt for AI Agents |
||
|
|
||
| def get_commodities_and_tech( | ||
| self, inp_comm: str | None, inp_tech: str | None, region: str | None | ||
|
|
@@ -171,7 +176,7 @@ def get_commodities_for_flags(self, flags: list[str] | None = None) -> set[str]: | |
| in_clause = ', '.join(f"'{flag}'" for flag in flags) | ||
| query = f'SELECT name FROM Commodity WHERE flag IN ({in_clause})' | ||
|
|
||
| return {row[0] for row in self.cur.execute(query)} | ||
| return {cast('str', row[0]) for row in self.cur.execute(query)} | ||
|
|
||
| def get_commodities_by_technology( | ||
| self, region: str | None, comm_type: str = 'input' | ||
|
|
@@ -187,11 +192,11 @@ def get_commodities_by_technology( | |
| if region: | ||
| query += f" WHERE region LIKE '%{region}%'" | ||
|
|
||
| return {tuple(row) for row in self.cur.execute(query)} | ||
| return {cast('tuple[str, str]', row) for row in self.cur.execute(query)} | ||
|
|
||
| def get_capacity_for_tech_and_period( | ||
| self, tech: str | None = None, period: int | None = None, region: str | None = None | ||
| ) -> pd.DataFrame | pd.Series: | ||
| ) -> pd.DataFrame | pd.Series[Any]: | ||
| """Retrieves capacity data, aggregated by technology.""" | ||
| if not self.scenario: | ||
| raise ValueError('A scenario must be set for output-related queries') | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential division by zero if
capsis empty.If
capsis empty after filtering,total_capwill be 0, causing aZeroDivisionErroron line 57.Suggested guard
🤖 Prompt for AI Agents