Skip to content

Commit

Permalink
Use generics to improve type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Nov 4, 2024
1 parent 7fc26d1 commit 751f378
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/aiidalab_qe/app/configuration/advanced/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .smearing import SmearingModel, SmearingSettings


class AdvancedSettings(SettingsPanel):
class AdvancedSettings(SettingsPanel[AdvancedModel]):
title = "Advanced Settings"
identifier = "advanced"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .model import HubbardModel


class HubbardSettings(AdvancedSubSettings):
class HubbardSettings(AdvancedSubSettings[HubbardModel]):
identifier = "hubbard"

def __init__(self, model: HubbardModel, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .model import MagnetizationModel


class MagnetizationSettings(AdvancedSubSettings):
class MagnetizationSettings(AdvancedSubSettings[MagnetizationModel]):
"""Widget to set the type of magnetization used in the calculation:
1) Tot_magnetization: Total majority spin charge - minority spin charge.
2) Starting magnetization: Starting spin polarization on atomic type 'i' in a spin polarized (LSDA or noncollinear/spin-orbit) calculation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
CutoffsPseudoPotentialFamily = GroupFactory("pseudo.family.cutoffs")


class PseudoSettings(AdvancedSubSettings):
class PseudoSettings(AdvancedSubSettings[PseudosModel]):
identifier = "pseudos"

def __init__(self, model: PseudosModel, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .model import SmearingModel


class SmearingSettings(AdvancedSubSettings):
class SmearingSettings(AdvancedSubSettings[SmearingModel]):
identifier = "smearing"

def __init__(self, model: SmearingModel, **kwargs):
Expand Down
8 changes: 6 additions & 2 deletions src/aiidalab_qe/app/configuration/advanced/subsettings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import typing as t

import ipywidgets as ipw
import traitlets as tl
Expand Down Expand Up @@ -26,10 +27,13 @@ def reset(self):
raise NotImplementedError


class AdvancedSubSettings(ipw.VBox):
M = t.TypeVar("M", bound=AdvancedSubModel)


class AdvancedSubSettings(ipw.VBox, t.Generic[M]):
identifier = "sub"

def __init__(self, model: AdvancedSubModel, **kwargs):
def __init__(self, model: M, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

self.loading_message = LoadingWidget(f"Loading {self.identifier} settings")
Expand Down
7 changes: 4 additions & 3 deletions src/aiidalab_qe/app/configuration/basic/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

import ipywidgets as ipw

from aiidalab_qe.common.panel import SettingsModel, SettingsPanel
from aiidalab_qe.app.configuration.basic.model import WorkChainModel
from aiidalab_qe.common.panel import SettingsPanel


class WorkChainSettings(SettingsPanel):
class WorkChainSettings(SettingsPanel[WorkChainModel]):
title = "Basic Settings"
identifier = "workchain"

def __init__(self, model: SettingsModel, **kwargs):
def __init__(self, model: WorkChainModel, **kwargs):
super().__init__(model, **kwargs)
self._model.observe(
self._on_input_structure_change,
Expand Down
8 changes: 6 additions & 2 deletions src/aiidalab_qe/common/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

import os
import typing as t

import ipywidgets as ipw
import traitlets as tl
Expand Down Expand Up @@ -100,11 +101,14 @@ def reset(self):
pass


class SettingsPanel(Panel):
M = t.TypeVar("M", bound=SettingsModel)


class SettingsPanel(Panel, t.Generic[M]):
title = "Settings"
description = ""

def __init__(self, model: SettingsModel, **kwargs):
def __init__(self, model: M, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

self.loading_message = LoadingWidget(f"Loading {self.identifier} settings")
Expand Down
3 changes: 2 additions & 1 deletion src/aiidalab_qe/plugins/bands/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import ipywidgets as ipw

from aiidalab_qe.common.panel import SettingsPanel
from aiidalab_qe.plugins.bands.model import BandsModel


class Setting(SettingsPanel):
class Setting(SettingsPanel[BandsModel]):
title = "Bands Structure"
identifier = "bands"

Expand Down
2 changes: 1 addition & 1 deletion src/aiidalab_qe/plugins/pdos/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
RYDBERG_TO_EV = 13.605703976


class Setting(SettingsPanel):
class Setting(SettingsPanel[PdosModel]):
title = "PDOS"
identifier = "pdos"

Expand Down
2 changes: 1 addition & 1 deletion src/aiidalab_qe/plugins/xas/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .model import XasModel


class Setting(SettingsPanel):
class Setting(SettingsPanel[XasModel]):
title = "XAS"
identifier = "xas"

Expand Down
2 changes: 1 addition & 1 deletion src/aiidalab_qe/plugins/xps/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .model import BASE_URL, XpsModel


class Setting(SettingsPanel):
class Setting(SettingsPanel[XpsModel]):
title = "XPS"
identifier = "xps"

Expand Down

0 comments on commit 751f378

Please sign in to comment.