From acc736acfcdd8df10e0a843c356becddd6233aba Mon Sep 17 00:00:00 2001 From: Hussain Jafari Date: Thu, 7 Nov 2024 10:33:17 -0800 Subject: [PATCH] drop python 3.9 support (#119) 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. --- .github/workflows/build.yml | 2 +- CHANGELOG.rst | 6 +++ docs/source/conf.py | 1 + setup.py | 1 + src/gbd_mapping/base_template.py | 7 ++-- src/gbd_mapping/cause_template.py | 12 +++--- src/gbd_mapping/covariate_template.py | 4 +- src/gbd_mapping/etiology_template.py | 4 +- src/gbd_mapping/risk_factor_template.py | 14 +++---- .../base_template_builder.py | 11 +++--- src/gbd_mapping_generator/cause_builder.py | 29 ++++++++------- .../covariate_builder.py | 20 ++++++---- src/gbd_mapping_generator/data.py | 10 ++--- src/gbd_mapping_generator/etiology_builder.py | 24 +++++++----- src/gbd_mapping_generator/globals.py | 6 +-- src/gbd_mapping_generator/id_builder.py | 12 +++--- src/gbd_mapping_generator/risk_builder.py | 37 ++++++++++--------- src/gbd_mapping_generator/sequela_builder.py | 29 ++++++++++----- src/gbd_mapping_generator/util.py | 27 +++++++------- 19 files changed, 139 insertions(+), 117 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 43d6c60..4d21324 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7959219..ba619d5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/docs/source/conf.py b/docs/source/conf.py index 82d3485..2e9e733 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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. diff --git a/setup.py b/setup.py index 703280f..70d14ee 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ doc_requirements = [ "sphinx>=6.2.1, <7.0", "sphinx-rtd-theme", + "sphinx-autodoc-typehints", ] lint_requirements = [ diff --git a/src/gbd_mapping/base_template.py b/src/gbd_mapping/base_template.py index 3f4205e..fd84153 100644 --- a/src/gbd_mapping/base_template.py +++ b/src/gbd_mapping/base_template.py @@ -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 @@ -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: @@ -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 diff --git a/src/gbd_mapping/cause_template.py b/src/gbd_mapping/cause_template.py index 646c1f8..5a4c977 100644 --- a/src/gbd_mapping/cause_template.py +++ b/src/gbd_mapping/cause_template.py @@ -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 @@ -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) diff --git a/src/gbd_mapping/covariate_template.py b/src/gbd_mapping/covariate_template.py index 0f628e1..97a466c 100644 --- a/src/gbd_mapping/covariate_template.py +++ b/src/gbd_mapping/covariate_template.py @@ -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 @@ -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, ): diff --git a/src/gbd_mapping/etiology_template.py b/src/gbd_mapping/etiology_template.py index 22b04dd..7147ec6 100644 --- a/src/gbd_mapping/etiology_template.py +++ b/src/gbd_mapping/etiology_template.py @@ -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 @@ -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) diff --git a/src/gbd_mapping/risk_factor_template.py b/src/gbd_mapping/risk_factor_template.py index fc401ae..837f55f 100644 --- a/src/gbd_mapping/risk_factor_template.py +++ b/src/gbd_mapping/risk_factor_template.py @@ -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 @@ -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, ): diff --git a/src/gbd_mapping_generator/base_template_builder.py b/src/gbd_mapping_generator/base_template_builder.py index 255eef6..e913569 100644 --- a/src/gbd_mapping_generator/base_template_builder.py +++ b/src/gbd_mapping_generator/base_template_builder.py @@ -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") @@ -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"), @@ -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: @@ -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", @@ -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 diff --git a/src/gbd_mapping_generator/cause_builder.py b/src/gbd_mapping_generator/cause_builder.py index b9f3617..b4cd5a8 100644 --- a/src/gbd_mapping_generator/cause_builder.py +++ b/src/gbd_mapping_generator/cause_builder.py @@ -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, @@ -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 { @@ -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 diff --git a/src/gbd_mapping_generator/covariate_builder.py b/src/gbd_mapping_generator/covariate_builder.py index 59c949d..26e1e3c 100644 --- a/src/gbd_mapping_generator/covariate_builder.py +++ b/src/gbd_mapping_generator/covariate_builder.py @@ -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") @@ -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"), @@ -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 diff --git a/src/gbd_mapping_generator/data.py b/src/gbd_mapping_generator/data.py index c3f66bf..78fc80b 100644 --- a/src/gbd_mapping_generator/data.py +++ b/src/gbd_mapping_generator/data.py @@ -1,5 +1,3 @@ -from typing import List - import numpy as np import pandas as pd @@ -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( @@ -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] @@ -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") @@ -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): diff --git a/src/gbd_mapping_generator/etiology_builder.py b/src/gbd_mapping_generator/etiology_builder.py index bb3a6e0..92742a6 100644 --- a/src/gbd_mapping_generator/etiology_builder.py +++ b/src/gbd_mapping_generator/etiology_builder.py @@ -1,9 +1,14 @@ -from typing import List, Tuple - from .base_template_builder import gbd_record_attrs, modelable_entity_attrs from .data import get_etiology_data, get_etiology_list from .globals import ID_TYPES -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 = ("Etiology", "etiologies") @@ -12,7 +17,7 @@ def get_base_types(): etiology_attrs = [ ("name", "str"), ("kind", "str"), - ("gbd_id", f"Union[{ID_TYPES.REI_ID}, None]"), + ("gbd_id", f"{ID_TYPES.REI_ID} | None"), ] return { @@ -39,7 +44,7 @@ def make_etiology(name: str, rei_id: float) -> str: return out -def make_etiologies(etiology_list: List[Tuple[str, float]]) -> str: +def make_etiologies(etiology_list: list[tuple[str, float]]) -> str: out = "etiologies = Etiologies(\n" for name, rei_id in etiology_list: out += make_etiology(name, rei_id) @@ -49,19 +54,18 @@ def make_etiologies(etiology_list: List[Tuple[str, float]]) -> str: def build_mapping_template() -> str: out = make_module_docstring("Mapping templates for GBD etiologies.", __file__) - out += make_import("typing", ("Union",)) + "\n" + out += make_import(".base_template", ("GbdRecord", "ModelableEntity")) out += make_import(".id", (ID_TYPES.REI_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() -> str: out = make_module_docstring("Mapping of GBD etiologies.", __file__) - out += make_import(".id", (ID_TYPES.REI_ID,)) - out += make_import(".etiology_template", ("Etiology", "Etiologies")) + SPACING + out += make_import(".etiology_template", ("Etiologies", "Etiology")) + out += make_import(".id", (ID_TYPES.REI_ID,)) + SINGLE_SPACING out += make_etiologies(get_etiology_data()) return out diff --git a/src/gbd_mapping_generator/globals.py b/src/gbd_mapping_generator/globals.py index 2537dac..dd03689 100644 --- a/src/gbd_mapping_generator/globals.py +++ b/src/gbd_mapping_generator/globals.py @@ -1,4 +1,4 @@ -from typing import List, NamedTuple, Tuple +from typing import NamedTuple class __IdTypes(NamedTuple): @@ -14,5 +14,5 @@ class __IdTypes(NamedTuple): # Type aliases -CovariateData = Tuple[str, float, bool, bool, bool] -CovariateDataSeq = List[CovariateData] +CovariateData = tuple[str, float, bool, bool, bool] +CovariateDataSeq = list[CovariateData] diff --git a/src/gbd_mapping_generator/id_builder.py b/src/gbd_mapping_generator/id_builder.py index 66b1671..dfcb7e3 100644 --- a/src/gbd_mapping_generator/id_builder.py +++ b/src/gbd_mapping_generator/id_builder.py @@ -1,5 +1,5 @@ from .globals import ID_TYPES -from .util import SPACING, TAB, make_module_docstring +from .util import DOUBLE_SPACING, TAB, make_module_docstring _ID_TYPES = ( (ID_TYPES.ME_ID, "Modelable Entity ID"), @@ -20,8 +20,8 @@ def make_unknown_flag(): out += "class Unknown:\n" out += TAB + '"""Marker for unknown values."""\n' out += TAB + "def __repr__(self):\n" - out += 2 * TAB + 'return "UNKNOWN"\n' + SPACING - out += "UNKNOWN = Unknown()\n" + SPACING + out += 2 * TAB + 'return "UNKNOWN"\n' + DOUBLE_SPACING + out += "UNKNOWN = Unknown()\n" + DOUBLE_SPACING out += "class UnknownEntityError(Exception):\n" out += ( TAB @@ -34,18 +34,18 @@ def make_unknown_flag(): def build_mapping(): out = make_module_docstring("Custom ID types for GBD entities", __file__) for k, v in _ID_TYPES: - out += SPACING + out += DOUBLE_SPACING out += f"class {k}(int):\n" out += TAB + f'"""{v}"""\n' out += TAB + "def __repr__(self):\n" out += 2 * TAB + f'return "{k}({{:d}})".format(self)\n' - out += SPACING + out += DOUBLE_SPACING out += "class scalar(float):\n" out += TAB + '"""Raw Measure Value"""\n' out += TAB + "def __repr__(self):\n" out += 2 * TAB + 'return "scalar({:f})".format(self)\n' - out += SPACING + out += DOUBLE_SPACING out += make_unknown_flag() diff --git a/src/gbd_mapping_generator/risk_builder.py b/src/gbd_mapping_generator/risk_builder.py index 572a18e..63612c4 100644 --- a/src/gbd_mapping_generator/risk_builder.py +++ b/src/gbd_mapping_generator/risk_builder.py @@ -1,10 +1,9 @@ -from typing import List - from .base_template_builder import gbd_record_attrs, modelable_entity_attrs from .data import get_risk_data, get_risk_list from .globals import ID_TYPES from .util import ( - SPACING, + DOUBLE_SPACING, + SINGLE_SPACING, TAB, TEXTWIDTH, format_string_none, @@ -24,17 +23,17 @@ def get_base_types(): ("gbd_id", ID_TYPES.REI_ID), ("level", "int"), ("most_detailed", "bool"), - ("distribution", "Union[str, None]"), + ("distribution", "str | None"), ("population_attributable_fraction_calculation_type", "str"), ("restrictions", "Restrictions"), ] risk_attrs += [ - ("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"), @@ -114,7 +113,7 @@ def make_risk( return out -def make_entity_list(name, entity_list: List, entity_type: str) -> str: +def make_entity_list(name, entity_list: list, entity_type: str) -> str: field = 2 * TAB + f"{name}=(" if not entity_list: return field + "),\n" @@ -138,7 +137,7 @@ def make_entity_list(name, entity_list: List, entity_type: str) -> str: return out -def make_risks(risk_list: List) -> str: +def make_risks(risk_list: list) -> str: out = "risk_factors = RiskFactors(\n" for ( name, @@ -195,25 +194,27 @@ def make_risks(risk_list: List) -> str: def build_mapping_template(): out = make_module_docstring("Mapping templates for GBD risk factors.", __file__) - out += make_import("typing", ("Tuple", "Union")) + "\n" - out += make_import(".id", (ID_TYPES.REI_ID, "scalar")) + out += make_import("__future__", ("annotations",)) + "\n" out += make_import( ".base_template", - ("ModelableEntity", "GbdRecord", "Categories", "Tmred", "Restrictions"), + ("Categories", "GbdRecord", "ModelableEntity", "Restrictions", "Tmred"), ) out += make_import(".cause_template", ("Cause",)) + out += make_import(".id", (ID_TYPES.REI_ID, "scalar")) for entity, info in get_base_types().items(): - out += SPACING + out += DOUBLE_SPACING out += make_record(entity, **info) return out def build_mapping() -> str: out = make_module_docstring("Mapping of GBD risk factors.", __file__) + out += make_import(".base_template", ("Categories", "Restrictions", "Tmred")) + out += make_import(".cause", ("causes",)) out += make_import(".id", (ID_TYPES.REI_ID, "scalar")) - out += make_import(".base_template", ("Categories", "Tmred", "Restrictions")) - out += make_import(".risk_factor_template", ("RiskFactor", "RiskFactors")) - out += make_import(".cause", ("causes",)) + SPACING + out += ( + make_import(".risk_factor_template", ("RiskFactor", "RiskFactors")) + SINGLE_SPACING + ) out += make_risks(get_risk_data()) return out diff --git a/src/gbd_mapping_generator/sequela_builder.py b/src/gbd_mapping_generator/sequela_builder.py index 33a42b1..c2d0ba2 100644 --- a/src/gbd_mapping_generator/sequela_builder.py +++ b/src/gbd_mapping_generator/sequela_builder.py @@ -1,11 +1,17 @@ -from typing import List - from .base_template_builder import gbd_record_attrs, modelable_entity_attrs from .data import get_sequela_data, get_sequela_list from .globals import ID_TYPES -from .util import SPACING, TAB, make_import, make_module_docstring, make_record, to_id +from .util import ( + DOUBLE_SPACING, + SINGLE_SPACING, + TAB, + make_import, + make_module_docstring, + make_record, + to_id, +) -IMPORTABLES_DEFINED = ("Sequela", "Healthstate", "sequelae") +IMPORTABLES_DEFINED = ("Healthstate", "Sequela", "sequelae") def get_base_types(): @@ -59,7 +65,7 @@ def make_sequela(name: str, s_id: float, mei_id: float, hs_name: str, hs_id: flo return out -def make_sequelae(sequela_list: List[str]) -> str: +def make_sequelae(sequela_list: list[str]) -> str: out = "sequelae = Sequelae(**{\n" for (name, sid, mei_id, hs_name, hsid) in sequela_list: out += make_sequela(name, sid, mei_id, hs_name, hsid) @@ -69,18 +75,21 @@ def make_sequelae(sequela_list: List[str]) -> str: def build_mapping_template() -> str: out = make_module_docstring("Mapping templates for GBD sequelae.", __file__) - out += make_import(".id", (ID_TYPES.S_ID, ID_TYPES.ME_ID, ID_TYPES.HS_ID)) - out += make_import(".base_template", ("ModelableEntity", "GbdRecord")) + out += make_import(".base_template", ("GbdRecord", "ModelableEntity")) + out += make_import(".id", (ID_TYPES.HS_ID, ID_TYPES.ME_ID, ID_TYPES.S_ID)) for entity, info in get_base_types().items(): - out += SPACING + out += DOUBLE_SPACING out += make_record(entity, **info) return out def build_mapping() -> str: out = make_module_docstring("Mapping of GBD sequelae.", __file__) - out += make_import(".id", (ID_TYPES.S_ID, ID_TYPES.HS_ID, ID_TYPES.ME_ID)) - out += make_import(".sequela_template", ("Healthstate", "Sequela", "Sequelae")) + SPACING + out += make_import(".id", (ID_TYPES.HS_ID, ID_TYPES.ME_ID, ID_TYPES.S_ID)) + out += ( + make_import(".sequela_template", ("Healthstate", "Sequela", "Sequelae")) + + SINGLE_SPACING + ) out += make_sequelae(get_sequela_data()) return out diff --git a/src/gbd_mapping_generator/util.py b/src/gbd_mapping_generator/util.py index c8c42e2..7764f0f 100644 --- a/src/gbd_mapping_generator/util.py +++ b/src/gbd_mapping_generator/util.py @@ -1,20 +1,21 @@ import os import re from pathlib import Path -from typing import Any, Dict, List, Tuple, Union +from typing import Any import numpy as np import pandas as pd -TEXTWIDTH = 118 # type: int -TAB = " " # type: str -SPACING = "\n\n" # type: str +TEXTWIDTH: int = 118 +TAB: str = " " +SINGLE_SPACING: str = "\n" +DOUBLE_SPACING: str = "\n\n" # In Python 3.7 and newer, there is no limit. This limit applies to positional and keyword arguments. MAX_PYTHON_3_6_ARG_COUNT = 255 # type: int -def make_module_docstring(description: str, file: Union[str, Path]) -> str: +def make_module_docstring(description: str, file: str | Path) -> str: """Generates standard header with additional information from the description. Parameters @@ -37,7 +38,7 @@ def make_module_docstring(description: str, file: Union[str, Path]) -> str: return out -def make_import(module_to_import: str, imports: Tuple[str, ...] = ()) -> str: +def make_import(module_to_import: str, imports: tuple[str, ...] = ()) -> str: """Generates the necessary imports. Smart about importing modules or names. Parameters @@ -114,7 +115,7 @@ def replace_numeric_prefix(name: str) -> str: return name -def clean_entity_list(raw_entity_series) -> List[str]: +def clean_entity_list(raw_entity_series) -> list[str]: replace_with_underscore_chars = [ "/", "(", @@ -218,7 +219,7 @@ def make_class_sig(name, superclass=None, docstring=None) -> str: return out -def make_slots(field_list: List[str]) -> str: +def make_slots(field_list: list[str]) -> str: """Generate explicit object attributes using slots (instead of dict). Parameters @@ -256,7 +257,7 @@ def make_slots(field_list: List[str]) -> str: return out -def make_init(field_dtype_tuples: Tuple[Tuple[str, str], ...]) -> str: +def make_init(field_dtype_tuples: tuple[tuple[str, str], ...]) -> str: """Generate the __init__ function as part of class generation. Parameters @@ -287,7 +288,7 @@ def make_init(field_dtype_tuples: Tuple[Tuple[str, str], ...]) -> str: return out -def make_super_call(superclass: Tuple[str, Tuple[Tuple[str, str], ...]]) -> str: +def make_super_call(superclass: tuple[str, tuple[tuple[str, str], ...]]) -> str: """Generate the call to initialize the parent class. Parameters @@ -316,7 +317,7 @@ def make_super_call(superclass: Tuple[str, Tuple[Tuple[str, str], ...]]) -> str: return out -def make_attribute_assignment(field_names: List[str]) -> str: +def make_attribute_assignment(field_names: list[str]) -> str: """Generate the class attributes and initialize them. Parameters @@ -344,8 +345,8 @@ def make_attribute_assignment(field_names: List[str]) -> str: def make_record( name: str, - attrs: Tuple[Tuple[str, str], ...] = None, - superclass: Tuple[str, Tuple[Tuple[str, str], ...]] = None, + attrs: tuple[tuple[str, str], ...] = None, + superclass: tuple[str, tuple[tuple[str, str], ...]] = None, docstring: str = None, ): """Generate class definitions from a name and additional information.