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

Clear up several mypy # type: ignores #123

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions ixmp4/core/optimization/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ixmp4.data.abstract import Docs as DocsModel
from ixmp4.data.abstract import Run
from ixmp4.data.abstract import Scalar as ScalarModel
from ixmp4.data.abstract import Unit as UnitModel


class Scalar(BaseModelFacade):
Expand Down Expand Up @@ -38,21 +39,19 @@ def value(self, value: float):
)

@property
def unit(self):
def unit(self) -> UnitModel:
"""Associated unit."""
return self._model.unit

@unit.setter
def unit(self, unit: str | Unit):
if isinstance(unit, Unit):
unit = unit
else:
unit_model = self.backend.units.get(unit)
unit = Unit(_backend=self.backend, _model=unit_model)
def unit(self, value: str | Unit):
if isinstance(value, str):
unit_model = self.backend.units.get(value)
value = Unit(_backend=self.backend, _model=unit_model)
self._model = self.backend.optimization.scalars.update(
id=self._model.id,
value=self._model.value,
unit_id=unit.id,
unit_id=value.id,
)

@property
Expand Down
3 changes: 2 additions & 1 deletion ixmp4/data/abstract/optimization/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .. import base
from ..docs import DocsRepository
from ..unit import Unit


class Scalar(base.BaseModel, Protocol):
Expand All @@ -17,7 +18,7 @@ class Scalar(base.BaseModel, Protocol):
"""Value of the Scalar."""
unit__id: types.Integer
"Foreign unique integer id of a unit."
unit: types.Mapped
unit: types.Mapped[Unit]
"Associated unit."
run__id: types.Integer
"Foreign unique integer id of a run."
Expand Down
2 changes: 1 addition & 1 deletion ixmp4/data/auth/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
if utils.is_joined(exc, Model):
perms = self.tabulate_permissions()
if perms.empty:
return exc.where(False) # type: ignore
return exc.where(db.false())

Check warning on line 50 in ixmp4/data/auth/context.py

View check run for this annotation

Codecov / codecov/patch

ixmp4/data/auth/context.py#L50

Added line #L50 was not covered by tests
if access_type == "edit":
perms = perms.where(perms["access_type"] == "EDIT").dropna()
# `*` is used as wildcard in permission logic, replaced by sql-wildcard `%`
Expand Down
1 change: 1 addition & 0 deletions ixmp4/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
UniqueConstraint,
delete,
exists,
false,
func,
insert,
or_,
Expand Down
8 changes: 5 additions & 3 deletions tests/core/test_optimization_indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ def test_get_indexset(self, platform: ixmp4.Platform):

def test_add_data(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
test_data = ["foo", "bar"]
# See https://mypy.readthedocs.io/en/stable/common_issues.html#variance for why
# a type hint is required here
test_data: list[float | int | str] = ["foo", "bar"]
indexset_1 = run.optimization.indexsets.create("Indexset 1")
indexset_1.add(test_data) # type: ignore
run.optimization.indexsets.create("Indexset 2").add(test_data) # type: ignore
indexset_1.add(test_data)
run.optimization.indexsets.create("Indexset 2").add(test_data)
indexset_2 = run.optimization.indexsets.get("Indexset 2")

assert indexset_1.data == indexset_2.data
Expand Down
9 changes: 4 additions & 5 deletions tests/core/test_optimization_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ def test_create_scalar(self, platform: ixmp4.Platform):
"Scalar 1", value=20, unit=unit.name
)

with pytest.raises(TypeError):
_ = run.optimization.scalars.create("Scalar 2") # type: ignore

scalar_2 = run.optimization.scalars.create("Scalar 2", value=20, unit=unit)
assert scalar_1.id != scalar_2.id

Expand Down Expand Up @@ -86,15 +83,17 @@ def test_update_scalar(self, platform: ixmp4.Platform):
_ = run.optimization.scalars.create("Scalar", value=20, unit=unit2.name)

scalar.value = 30
scalar.unit = "Test Unit"
# At the moment, mypy doesn't allow for different types in property getter and
# setter, see https://github.com/python/mypy/issues/3004
scalar.unit = "Test Unit" # type: ignore
# NOTE: doesn't work for some reason (but doesn't either for e.g. model.get())
# assert scalar == run.optimization.scalars.get("Scalar")
result = run.optimization.scalars.get("Scalar")

assert scalar.id == result.id
assert scalar.name == result.name
assert scalar.value == result.value == 30
assert scalar.unit.id == result.unit.id == 1 # type: ignore
assert scalar.unit.id == result.unit.id == 1

def test_list_scalars(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_optimization_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_create_table(self, platform: ixmp4.Platform):

# Test normal creation
indexset, indexset_2 = tuple(
IndexSet(_backend=platform.backend, _model=model) # type: ignore
IndexSet(_backend=platform.backend, _model=model)
for model in create_indexsets_for_run(platform=platform, run_id=run.id)
)
table = run.optimization.tables.create(
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_get_table(self, platform: ixmp4.Platform):
def test_table_add_data(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
indexset, indexset_2 = tuple(
IndexSet(_backend=platform.backend, _model=model) # type: ignore
IndexSet(_backend=platform.backend, _model=model)
for model in create_indexsets_for_run(platform=platform, run_id=run.id)
)
indexset.add(data=["foo", "bar", ""])
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_list_tables(self, platform: ixmp4.Platform):
def test_tabulate_table(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
indexset, indexset_2 = tuple(
IndexSet(_backend=platform.backend, _model=model) # type: ignore
IndexSet(_backend=platform.backend, _model=model)
for model in create_indexsets_for_run(platform=platform, run_id=run.id)
)
table = run.optimization.tables.create(
Expand Down
17 changes: 15 additions & 2 deletions tests/data/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Literal

import pandas as pd
import pytest

Expand All @@ -7,7 +9,18 @@

from ..utils import assert_unordered_equality

TEST_ENTRIES = [
TEST_ENTRIES: list[
tuple[
str,
bool | float | int | str,
Literal[
RunMetaEntry.Type.BOOL,
RunMetaEntry.Type.FLOAT,
RunMetaEntry.Type.INT,
RunMetaEntry.Type.STR,
],
]
] = [
("Boolean", True, RunMetaEntry.Type.BOOL),
("Float", 0.2, RunMetaEntry.Type.FLOAT),
("Integer", 1, RunMetaEntry.Type.INT),
Expand All @@ -26,7 +39,7 @@ def test_create_get_entry(self, platform: ixmp4.Platform):
run.set_as_default()

for key, value, type in TEST_ENTRIES:
entry = platform.backend.meta.create(run.id, key, value) # type:ignore
entry = platform.backend.meta.create(run.id, key, value)
assert entry.key == key
assert entry.value == value
assert entry.type == type
Expand Down
8 changes: 5 additions & 3 deletions tests/data/test_optimization_indexset.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,24 @@ def test_tabulate_indexsets(self, platform: ixmp4.Platform):
)

def test_add_data(self, platform: ixmp4.Platform):
test_data = ["foo", "bar"]
# See https://mypy.readthedocs.io/en/stable/common_issues.html#variance for why
# a type hint is required here
test_data: list[float | int | str] = ["foo", "bar"]
run = platform.backend.runs.create("Model", "Scenario")
indexset_1, indexset_2 = create_indexsets_for_run(
platform=platform, run_id=run.id
)
platform.backend.optimization.indexsets.add_data(
indexset_id=indexset_1.id,
data=test_data, # type: ignore
data=test_data,
)
indexset_1 = platform.backend.optimization.indexsets.get(
run_id=run.id, name=indexset_1.name
)

platform.backend.optimization.indexsets.add_data(
indexset_id=indexset_2.id,
data=test_data, # type: ignore
data=test_data,
)

assert (
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def create_indexsets_for_run(
return tuple(
platform.backend.optimization.indexsets.create(
run_id=run_id, name=f"Indexset {i}"
) # type: ignore
)
for i in range(offset, offset + amount)
)
Loading