Skip to content

Commit

Permalink
drop python 3.9 support (#119)
Browse files Browse the repository at this point in the history
Category: other
JIRA issue: MIC-5489

Changes and notes
Drop Python 3.9 support.
Use built in list, dicts, and tuples
Use pipe syntax instead of Union or Optional.

Testing
Built templates and compared to current templates.
  • Loading branch information
hussain-jafari authored Nov 7, 2024
1 parent 8c46ed5 commit acc736a
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.10", "3.11"]
uses:
ihmeuw/vivarium_build_utils/.github/workflows/build.yml@main
with:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
**4.1.0 - 11/06/24**

- Drop support for Python 3.9
- Modernize type hints
- Update imports written by builders to be isort compliant

**4.0.0 - 05/20/24**

- Rebuild with GBD 2021 data
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx_autodoc_typehints",
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
doc_requirements = [
"sphinx>=6.2.1, <7.0",
"sphinx-rtd-theme",
"sphinx-autodoc-typehints",
]

lint_requirements = [
Expand Down
7 changes: 3 additions & 4 deletions src/gbd_mapping/base_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Any manual changes will be lost.
"""
from typing import Tuple, Union

from .id import c_id, cov_id, hs_id, me_id, rei_id, s_id, scalar


Expand All @@ -19,7 +17,7 @@ def to_dict(self):
attr = getattr(self, item)
if isinstance(attr, GbdRecord):
out[item] = attr.to_dict()
elif isinstance(attr, Tuple) and attr:
elif isinstance(attr, tuple) and attr:
if isinstance(attr[0], GbdRecord):
out[item] = tuple(r.to_dict() for r in attr)
elif attr is not None:
Expand Down Expand Up @@ -70,7 +68,8 @@ class ModelableEntity(GbdRecord):
def __init__(self,
name: str,
kind: str,
gbd_id: Union[c_id, s_id, hs_id, me_id, cov_id, rei_id, None], ):
gbd_id: c_id | s_id | hs_id | me_id | cov_id | rei_id | None,
):
super().__init__()
self.name = name
self.kind = kind
Expand Down
12 changes: 6 additions & 6 deletions src/gbd_mapping/cause_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Any manual changes will be lost.
"""
from typing import Tuple, Union
from __future__ import annotations

from .base_template import GbdRecord, ModelableEntity, Restrictions
from .etiology_template import Etiology
Expand All @@ -21,14 +21,14 @@ def __init__(self,
name: str,
kind: str,
gbd_id: c_id,
me_id: Union[me_id, Unknown],
me_id: me_id | Unknown,
most_detailed: bool,
level: int,
restrictions: Restrictions,
parent: "Cause" = None,
sub_causes: Tuple["Cause", ...] = None,
sequelae: Tuple[Sequela, ...] = None,
etiologies: Tuple[Etiology, ...] = None, ):
parent: Cause | None = None,
sub_causes: tuple[Cause, ...] | None = None,
sequelae: tuple[Sequela, ...] | None = None,
etiologies: tuple[Etiology, ...] | None = None, ):
super().__init__(name=name,
kind=kind,
gbd_id=gbd_id)
Expand Down
4 changes: 1 addition & 3 deletions src/gbd_mapping/covariate_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Any manual changes will be lost.
"""
from typing import Union

from .base_template import GbdRecord, ModelableEntity
from .id import cov_id

Expand All @@ -17,7 +15,7 @@ class Covariate(ModelableEntity):
def __init__(self,
name: str,
kind: str,
gbd_id: Union[cov_id, None],
gbd_id: cov_id | None,
by_age: bool,
by_sex: bool,
dichotomous: bool, ):
Expand Down
4 changes: 1 addition & 3 deletions src/gbd_mapping/etiology_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Any manual changes will be lost.
"""
from typing import Union

from .base_template import GbdRecord, ModelableEntity
from .id import rei_id

Expand All @@ -17,7 +15,7 @@ class Etiology(ModelableEntity):
def __init__(self,
name: str,
kind: str,
gbd_id: Union[rei_id, None], ):
gbd_id: rei_id | None, ):
super().__init__(name=name,
kind=kind,
gbd_id=gbd_id)
Expand Down
14 changes: 7 additions & 7 deletions src/gbd_mapping/risk_factor_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Any manual changes will be lost.
"""
from typing import Tuple, Union
from __future__ import annotations

from .base_template import Categories, GbdRecord, ModelableEntity, Restrictions, Tmred
from .cause_template import Cause
Expand All @@ -24,14 +24,14 @@ def __init__(self,
gbd_id: rei_id,
level: int,
most_detailed: bool,
distribution: Union[str, None],
distribution: str | None,
population_attributable_fraction_calculation_type: str,
restrictions: Restrictions,
affected_causes: Tuple[Cause, ...],
population_attributable_fraction_of_one_causes: Tuple[Cause, ...],
parent: Union["RiskFactor", None] = None,
sub_risk_factors: Tuple["RiskFactor", ...] = None,
affected_risk_factors: Tuple["RiskFactor", ...] = None,
affected_causes: tuple[Cause, ...],
population_attributable_fraction_of_one_causes: tuple[Cause, ...],
parent: RiskFactor | None = None,
sub_risk_factors: tuple[RiskFactor, ...] = None,
affected_risk_factors: tuple[RiskFactor, ...] = None,
categories: Categories = None,
tmred: Tmred = None,
relative_risk_scalar: scalar = None, ):
Expand Down
11 changes: 5 additions & 6 deletions src/gbd_mapping_generator/base_template_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .util import SPACING, make_import, make_module_docstring, make_record
from .util import DOUBLE_SPACING, make_import, make_module_docstring, make_record

IMPORTABLES_DEFINED = ("GbdRecord", "ModelableEntity", "Restrictions", "Tmred", "Categories")

Expand All @@ -7,7 +7,7 @@
modelable_entity_attrs = (
("name", "str"),
("kind", "str"),
("gbd_id", "Union[c_id, s_id, hs_id, me_id, cov_id, rei_id, None]"),
("gbd_id", "c_id | s_id | hs_id | me_id | cov_id | rei_id | None"),
)
restrictions_attrs = (
("male_only", "bool"),
Expand Down Expand Up @@ -64,7 +64,7 @@ def to_dict(self):
attr = getattr(self, item)
if isinstance(attr, GbdRecord):
out[item] = attr.to_dict()
elif isinstance(attr, Tuple) and attr:
elif isinstance(attr, tuple) and attr:
if isinstance(attr[0], GbdRecord):
out[item] = tuple(r.to_dict() for r in attr)
elif attr is not None:
Expand Down Expand Up @@ -121,7 +121,6 @@ def build_mapping() -> str:
"""
templates = make_module_docstring("Template classes for GBD entities", __file__)
templates += make_import("typing", ["Union", "Tuple"])
templates += (
make_import(
".id",
Expand All @@ -135,12 +134,12 @@ def build_mapping() -> str:
"scalar",
],
)
+ SPACING
+ DOUBLE_SPACING
)
templates += make_gbd_record()

for entity, info in get_base_types().items():
templates += SPACING
templates += DOUBLE_SPACING
templates += make_record(entity, **info)

return templates
29 changes: 15 additions & 14 deletions src/gbd_mapping_generator/cause_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from .data import get_cause_data, get_cause_list
from .globals import ID_TYPES
from .util import (
SPACING,
DOUBLE_SPACING,
SINGLE_SPACING,
TAB,
TEXTWIDTH,
make_import,
Expand All @@ -21,16 +22,16 @@ def get_base_types():
("name", "str"),
("kind", "str"),
("gbd_id", ID_TYPES.C_ID),
("me_id", f"Union[{ID_TYPES.ME_ID}, Unknown]"),
("me_id", f"{ID_TYPES.ME_ID} | Unknown"),
("most_detailed", "bool"),
("level", "int"),
("restrictions", "Restrictions"),
]
cause_attrs += [
("parent", '"Cause" = None'),
("sub_causes", 'Tuple["Cause", ...] = None'),
("sequelae", "Tuple[Sequela, ...] = None"),
("etiologies", "Tuple[Etiology, ...] = None"),
("parent", "Cause | None = None"),
("sub_causes", "tuple[Cause, ...] | None = None"),
("sequelae", "tuple[Sequela, ...] | None = None"),
("etiologies", "tuple[Etiology, ...] | None = None"),
]

return {
Expand Down Expand Up @@ -140,24 +141,24 @@ def make_causes(causes_list):

def build_mapping_template():
out = make_module_docstring("Mapping templates for GBD causes.", __file__)
out += make_import("typing", ("Union", "Tuple")) + "\n"
out += make_import(".id", (ID_TYPES.C_ID, ID_TYPES.ME_ID, "Unknown"))
out += make_import(".base_template", ("Restrictions", "ModelableEntity", "GbdRecord"))
out += make_import(".sequela_template", ("Sequela",))
out += make_import("__future__", ("annotations",)) + "\n"
out += make_import(".base_template", ("GbdRecord", "ModelableEntity", "Restrictions"))
out += make_import(".etiology_template", ("Etiology",))
out += make_import(".id", ("Unknown", ID_TYPES.C_ID, ID_TYPES.ME_ID))
out += make_import(".sequela_template", ("Sequela",))

for entity, info in get_base_types().items():
out += SPACING
out += DOUBLE_SPACING
out += make_record(entity, **info)
return out


def build_mapping():
out = make_module_docstring("Mapping of GBD causes.", __file__)
out += make_import(".id", (ID_TYPES.C_ID, ID_TYPES.ME_ID, "UNKNOWN"))
out += make_import(".base_template", ("Restrictions",))
out += make_import(".cause_template", ("Cause", "Causes"))
out += make_import(".sequela", ("sequelae",))
out += make_import(".etiology", ("etiologies",)) + SPACING
out += make_import(".etiology", ("etiologies",))
out += make_import(".id", ("UNKNOWN", ID_TYPES.C_ID, ID_TYPES.ME_ID))
out += make_import(".sequela", ("sequelae",)) + SINGLE_SPACING
out += make_causes(get_cause_data())
return out
20 changes: 13 additions & 7 deletions src/gbd_mapping_generator/covariate_builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from .base_template_builder import gbd_record_attrs, modelable_entity_attrs
from .data import get_covariate_data, get_covariate_list
from .globals import ID_TYPES, CovariateDataSeq
from .util import SPACING, TAB, make_import, make_module_docstring, make_record
from .util import (
DOUBLE_SPACING,
SINGLE_SPACING,
TAB,
make_import,
make_module_docstring,
make_record,
)

IMPORTABLES_DEFINED = ("Covariate", "covariates")

Expand All @@ -10,7 +17,7 @@ def get_base_types():
cov_attrs = [
("name", "str"),
("kind", "str"),
("gbd_id", f"Union[{ID_TYPES.COV_ID}, None]"),
("gbd_id", f"{ID_TYPES.COV_ID} | None"),
("by_age", "bool"),
("by_sex", "bool"),
("dichotomous", "bool"),
Expand Down Expand Up @@ -90,19 +97,18 @@ def make_covariates(covariate_list: CovariateDataSeq) -> str:

def build_mapping_template():
out = make_module_docstring("Mapping templates for GBD covariates.", __file__)
out += make_import("typing", ("Union",)) + "\n"
out += make_import(".base_template", ("GbdRecord", "ModelableEntity"))
out += make_import(".id", (ID_TYPES.COV_ID,))
out += make_import(".base_template", ("ModelableEntity", "GbdRecord"))

for entity, info in get_base_types().items():
out += SPACING
out += DOUBLE_SPACING
out += make_record(entity, **info)
return out


def build_mapping():
out = make_module_docstring("Mapping of GBD covariates.", __file__)
out += make_import(".id", (ID_TYPES.COV_ID,))
out += make_import(".covariate_template", ("Covariate", "Covariates")) + SPACING
out += make_import(".covariate_template", ("Covariate", "Covariates"))
out += make_import(".id", (ID_TYPES.COV_ID,)) + SINGLE_SPACING
out += make_covariates(get_covariate_data())
return out
10 changes: 4 additions & 6 deletions src/gbd_mapping_generator/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

import numpy as np
import pandas as pd

Expand Down Expand Up @@ -120,7 +118,7 @@ def get_covariate_list(with_survey=False):
#####################################################


def get_sequela_data() -> List:
def get_sequela_data() -> list:
sequelae = gbd.get_sequela_id_mapping()

return list(
Expand All @@ -134,7 +132,7 @@ def get_sequela_data() -> List:
)


def get_etiology_data() -> List:
def get_etiology_data() -> list:
etiologies = gbd.get_rei_metadata(rei_set_id=ETIOLOGY_SET_ID)
etiologies = etiologies[etiologies["most_detailed"] == 1]

Expand Down Expand Up @@ -325,7 +323,7 @@ def get_all_risk_metadata():
return risks


def get_risk_data() -> List:
def get_risk_data() -> list:
risks = get_all_risk_metadata()
causes = get_causes().set_index("cause_id")

Expand Down Expand Up @@ -490,7 +488,7 @@ def get_risk_data() -> List:
return out


def get_duplicate_indices(names: List[str]) -> List[int]:
def get_duplicate_indices(names: list[str]) -> list[int]:
dup_indices = []
check = set()
for i, v in enumerate(names):
Expand Down
Loading

0 comments on commit acc736a

Please sign in to comment.