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

Modernize type annotations #1074

Merged
merged 4 commits into from
Mar 8, 2024
Merged
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
64 changes: 31 additions & 33 deletions .github/workflows/dummy_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,65 +26,63 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Lint with ruff
run: |
echo "pass!"
- name: Lint with ruff
run: |
echo "pass!"

format_with_black:

runs-on: ubuntu-latest

steps:
- name: Format with black
run: |
echo "pass!"
- name: Format with black
run: |
echo "pass!"

check_with_pydoclint:

runs-on: ubuntu-latest

steps:
- name: Check with pydoclint
run: |
echo "pass!"
- name: Check with pydoclint
run: |
echo "pass!"

test_minimal_dependencies:

runs-on: ubuntu-latest

steps:
- name: Test import
run: |
echo "pass!"
- name: Test import
run: |
echo "pass!"

test_build_docs:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install poetry
run: pipx install poetry
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'poetry'
- name: Install dependencies
run: |
poetry env use 3.10
poetry install --no-interaction --no-root --all-extras --with docs,torchcpu --without ci,lint,format,doclint
- name: Install pandoc
run: sudo apt-get install -y pandoc
- name: Build with sphinx
run: |
poetry run sphinx-build -W -b html ./docs ./docs/_build
- uses: actions/checkout@v3
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'poetry'
- name: Install dependencies
run: |
poetry env use 3.10
poetry install --no-interaction --no-root --all-extras --with torchcpu,docs --without ci,lint,doclint
- name: Install pandoc
run: sudo apt-get install -y pandoc
- name: Build with sphinx
run: |
poetry run sphinx-build -W -b html ./docs ./docs/_build

test_with_pytest:

runs-on: ubuntu-latest

steps:
- name: Test with pytest
run: |
echo "pass!"
- name: Test with pytest
run: |
echo "pass!"
5 changes: 2 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#
import os
import sys
from typing import List

import toml

Expand Down Expand Up @@ -87,7 +86,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns: List[str] = []
exclude_patterns: list[str] = []


# -- Options for HTML output -------------------------------------------------
Expand All @@ -101,4 +100,4 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path: List[str] = []
html_static_path: list[str] = []
4 changes: 2 additions & 2 deletions ethicml/data/csvs/make_nursery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# The Heritage Health dataset. It needs some (mild) preprocessing before we can plug and play.

from typing import Hashable, List
from collections.abc import Hashable

import pandas as pd

Expand Down Expand Up @@ -31,7 +31,7 @@
for column in features:
df[column] = df[column].astype("category").cat.codes

features1: List[Hashable] = [
features1: list[Hashable] = [
"form",
"health",
"finance",
Expand Down
3 changes: 2 additions & 1 deletion ethicml/data/dataset.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Data structure for all datasets that come with the framework."""

from abc import ABC, abstractmethod
from collections.abc import Sequence
from dataclasses import dataclass, field
from enum import auto
from pathlib import Path
import typing
from typing import ClassVar, Sequence, TypedDict, final
from typing import ClassVar, TypedDict, final
from typing_extensions import override

import pandas as pd
Expand Down
9 changes: 4 additions & 5 deletions ethicml/data/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional

import pandas as pd

Expand All @@ -28,10 +27,10 @@ def load_data(dataset: Dataset) -> DataTuple:
class ConfigurableDataset(LegacyDataset):
"""A configurable dataset class."""

filepath_: Optional[Path] = None
s_column: Optional[str] = None
y_column: Optional[str] = None
additional_to_drop: Optional[List[str]] = None
filepath_: Path | None = None
s_column: str | None = None
y_column: str | None = None
additional_to_drop: list[str] | None = None

def __post_init__(self) -> None:
assert (filepath := self.filepath_) is not None
Expand Down
2 changes: 1 addition & 1 deletion ethicml/data/lookup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Lookup tables / switch statements for project level objects."""

from typing import Callable
from collections.abc import Callable

from .dataset import Dataset
from .tabular_data.adult import Adult
Expand Down
5 changes: 3 additions & 2 deletions ethicml/data/tabular_data/acs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
Paper: https://arxiv.org/abs/2108.04884
"""

from collections.abc import Generator
import contextlib
import os
from pathlib import Path
from typing import Generator, Literal, get_args
from typing_extensions import TypeAlias, override
from typing import Literal, TypeAlias, get_args
from typing_extensions import override

import numpy as np
import pandas as pd
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/admissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import flatten_dict
Expand All @@ -61,7 +61,7 @@ class Admissions(LegacyDataset):

split: AdmissionsSplits = AdmissionsSplits.GENDER

Splits: ClassVar[Type[AdmissionsSplits]] = AdmissionsSplits
Splits: ClassVar[type[AdmissionsSplits]] = AdmissionsSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/adult.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Final, Type
from typing import ClassVar, Final
from typing_extensions import override

from ..dataset import LabelSpecsPair, StaticCSVDataset
Expand Down Expand Up @@ -39,7 +39,7 @@ class Adult(StaticCSVDataset):
:param binarize_race: If True, race will be white vs rest. (Default: False)
"""

Splits: ClassVar[Type[AdultSplits]] = AdultSplits
Splits: ClassVar[type[AdultSplits]] = AdultSplits
num_samples: ClassVar[int] = 45_222
csv_file: ClassVar[str] = "adult.csv.zip"

Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/compas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import LabelSpec, flatten_dict, spec_from_binary_cols
Expand All @@ -25,7 +25,7 @@ class Compas(LegacyDataset):

split: CompasSplits = CompasSplits.SEX

Splits: ClassVar[Type[CompasSplits]] = CompasSplits
Splits: ClassVar[type[CompasSplits]] = CompasSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/credit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import flatten_dict
Expand All @@ -23,7 +23,7 @@ class Credit(LegacyDataset):

split: CreditSplits = CreditSplits.SEX

Splits: ClassVar[Type[CreditSplits]] = CreditSplits
Splits: ClassVar[type[CreditSplits]] = CreditSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/crime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import filter_features_by_prefixes, flatten_dict
Expand All @@ -23,7 +23,7 @@ class Crime(LegacyDataset):

split: CrimeSplits = CrimeSplits.RACE_BINARY

Splits: ClassVar[Type[CrimeSplits]] = CrimeSplits
Splits: ClassVar[type[CrimeSplits]] = CrimeSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/german.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import flatten_dict
Expand All @@ -23,7 +23,7 @@ class German(LegacyDataset):

split: GermanSplits = GermanSplits.SEX

Splits: ClassVar[Type[GermanSplits]] = GermanSplits
Splits: ClassVar[type[GermanSplits]] = GermanSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import flatten_dict
Expand All @@ -23,7 +23,7 @@ class Health(LegacyDataset):

split: HealthSplits = HealthSplits.SEX

Splits: ClassVar[Type[HealthSplits]] = HealthSplits
Splits: ClassVar[type[HealthSplits]] = HealthSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
5 changes: 3 additions & 2 deletions ethicml/data/tabular_data/law.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
}
"""

from collections.abc import Mapping
from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Mapping, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import LabelGroup, flatten_dict, spec_from_binary_cols
Expand All @@ -46,7 +47,7 @@ class Law(LegacyDataset):

split: LawSplits = LawSplits.SEX

Splits: ClassVar[Type[LawSplits]] = LawSplits
Splits: ClassVar[type[LawSplits]] = LawSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/nursery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import LabelSpec, flatten_dict
Expand All @@ -28,7 +28,7 @@ class Nursery(LegacyDataset):
:param binarize_race: If True, race will be white vs rest. (Default: False)
"""

Splits: ClassVar[Type[NurserySplits]] = NurserySplits
Splits: ClassVar[type[NurserySplits]] = NurserySplits

split: NurserySplits = Splits.FINANCE

Expand Down
4 changes: 2 additions & 2 deletions ethicml/data/tabular_data/sqf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Type
from typing import ClassVar

from ..dataset import LegacyDataset
from ..util import LabelSpec, flatten_dict, spec_from_binary_cols
Expand All @@ -29,7 +29,7 @@ class Sqf(LegacyDataset):

split: SqfSplits = SqfSplits.SEX

Splits: ClassVar[Type[SqfSplits]] = SqfSplits
Splits: ClassVar[type[SqfSplits]] = SqfSplits
"""Shorthand for the Enum that defines the splits associated with this class."""

def __post_init__(self) -> None:
Expand Down
Loading
Loading