Skip to content

Commit

Permalink
Modernize all Python 3.8 annotations (#1815)
Browse files Browse the repository at this point in the history
* Use Python 3.9 as the 'base' CI version for linting

* Update pre-commit hooks

* Run `pyupgrade --py39-plus` to upgrade legacy annotations

* Add `--exit-non-zero-on-fix` for ruff

Co-authored-by: Casper Welzel Andersen <43357585+CasperWA@users.noreply.github.com>

* Use f-string over format

Co-authored-by: Casper Welzel Andersen <43357585+CasperWA@users.noreply.github.com>

---------

Co-authored-by: Casper Welzel Andersen <43357585+CasperWA@users.noreply.github.com>
  • Loading branch information
ml-evs and CasperWA committed Nov 6, 2023
1 parent 4ef9991 commit a3d1a5f
Show file tree
Hide file tree
Showing 62 changed files with 430 additions and 433 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:
with:
submodules: true

- name: Set up Python 3.10
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.9'
cache: 'pip'
cache-dependency-path: |
requirements*.txt
Expand All @@ -56,10 +56,10 @@ jobs:
with:
submodules: true

- name: Set up Python 3.10
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.9'
cache: 'pip'
cache-dependency-path: |
requirements*.txt
Expand Down Expand Up @@ -227,27 +227,27 @@ jobs:
run: pytest -rs -vvv --cov=./optimade/ --cov-report=xml --cov-append tests/adapters/

- name: Run tests for validator only to assess coverage (mongomock)
if: matrix.python-version == 3.10
if: matrix.python-version == 3.9
run: pytest -rs --cov=./optimade/ --cov-report=xml:validator_cov.xml --cov-append tests/server/test_server_validation.py
env:
OPTIMADE_DATABASE_BACKEND: 'mongomock'

- name: Run tests for validator only to assess coverage (Elasticsearch)
if: matrix.python-version == 3.10
if: matrix.python-version == 3.9
run: pytest -rs --cov=./optimade/ --cov-report=xml:validator_cov.xml --cov-append tests/server/test_server_validation.py
env:
OPTIMADE_DATABASE_BACKEND: 'elastic'
OPTIMADE_INSERT_TEST_DATA: false # Must be specified as previous steps will have already inserted the test data

- name: Run tests for validator only to assess coverage (MongoDB)
if: matrix.python-version == 3.10
if: matrix.python-version == 3.9
run: pytest -rs --cov=./optimade/ --cov-report=xml:validator_cov.xml --cov-append tests/server/test_server_validation.py
env:
OPTIMADE_DATABASE_BACKEND: 'mongodb'
OPTIMADE_INSERT_TEST_DATA: false # Must be specified as previous steps will have already inserted the test data

- name: Run the OPTIMADE Client CLI
if: matrix.python-version == 3.10
if: matrix.python-version == 3.9
run: |
coverage run --append --source optimade optimade/client/cli.py \
--filter 'nsites = 1' \
Expand Down Expand Up @@ -275,15 +275,15 @@ jobs:
coverage xml
- name: Upload coverage to Codecov
if: matrix.python-version == '3.10' && github.repository == 'Materials-Consortia/optimade-python-tools'
if: matrix.python-version == '3.9' && github.repository == 'Materials-Consortia/optimade-python-tools'
uses: codecov/codecov-action@v3
with:
name: project
file: ./coverage.xml
flags: project

- name: Upload validator coverage to Codecov
if: matrix.python-version == '3.10' && github.repository == 'Materials-Consortia/optimade-python-tools'
if: matrix.python-version == '3.9' && github.repository == 'Materials-Consortia/optimade-python-tools'
uses: codecov/codecov-action@v3
with:
name: validator
Expand All @@ -300,7 +300,7 @@ jobs:

- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.9'
cache: 'pip'
cache-dependency-path: |
requirements*.txt
Expand Down Expand Up @@ -330,10 +330,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python 3.10
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.9'
cache: 'pip'
cache-dependency-path: |
requirements*.txt
Expand Down
16 changes: 11 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.10
python: python3.9

repos:
- repo: https://github.com/ambv/black
Expand All @@ -9,7 +9,7 @@ repos:
name: Blacken

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-symlinks
- id: check-yaml
Expand All @@ -23,11 +23,17 @@ repos:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py39-plus"]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.291'
rev: 'v0.0.292'
hooks:
- id: ruff
args: [--fix]
args: [--fix, --exit-non-zero-on-fix]

- repo: local
hooks:
Expand All @@ -46,7 +52,7 @@ repos:
description: Update the API Reference documentation whenever a Python file is touched in the code base.

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.6.0
hooks:
- id: mypy
name: "MyPy"
Expand Down
14 changes: 7 additions & 7 deletions optimade/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
and [`StructureResource`][optimade.models.structures.StructureResource]s, respectively.
"""
import re
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, Optional, Union

from pydantic import BaseModel # pylint: disable=no-name-in-module

Expand All @@ -42,18 +42,18 @@ class EntryAdapter:
"""

ENTRY_RESOURCE: Type[EntryResource] = EntryResource
_type_converters: Dict[str, Callable] = {}
_type_ingesters: Dict[str, Callable] = {}
_type_ingesters_by_type: Dict[str, Type] = {}
ENTRY_RESOURCE: type[EntryResource] = EntryResource
_type_converters: dict[str, Callable] = {}
_type_ingesters: dict[str, Callable] = {}
_type_ingesters_by_type: dict[str, type] = {}

def __init__(self, entry: dict) -> None:
"""
Parameters:
entry (dict): A JSON OPTIMADE single resource entry.
"""
self._entry: Optional[EntryResource] = None
self._converted: Dict[str, Any] = {}
self._converted: dict[str, Any] = {}

self.entry: EntryResource = entry # type: ignore[assignment]

Expand Down Expand Up @@ -164,7 +164,7 @@ def ingest_from(cls, data: Any, format: Optional[str] = None) -> Any:

@staticmethod
def _get_model_attributes(
starting_instances: Union[Tuple[BaseModel, ...], List[BaseModel]], name: str
starting_instances: Union[tuple[BaseModel, ...], list[BaseModel]], name: str
) -> Any:
"""Helper method for retrieving the OPTIMADE model's attribute, supporting "."-nested attributes"""
for res in starting_instances:
Expand Down
4 changes: 1 addition & 3 deletions optimade/adapters/references/adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Type

from optimade.adapters.base import EntryAdapter
from optimade.models import ReferenceResource

Expand All @@ -21,4 +19,4 @@ class Reference(EntryAdapter):
"""

ENTRY_RESOURCE: Type[ReferenceResource] = ReferenceResource
ENTRY_RESOURCE: type[ReferenceResource] = ReferenceResource
10 changes: 5 additions & 5 deletions optimade/adapters/structures/adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Dict, Type
from typing import Callable

from optimade.adapters.base import EntryAdapter
from optimade.models import StructureResource
Expand Down Expand Up @@ -44,8 +44,8 @@ class Structure(EntryAdapter):
"""

ENTRY_RESOURCE: Type[StructureResource] = StructureResource
_type_converters: Dict[str, Callable] = {
ENTRY_RESOURCE: type[StructureResource] = StructureResource
_type_converters: dict[str, Callable] = {
"aiida_structuredata": get_aiida_structure_data,
"ase": get_ase_atoms,
"cif": get_cif,
Expand All @@ -55,12 +55,12 @@ class Structure(EntryAdapter):
"jarvis": get_jarvis_atoms,
}

_type_ingesters: Dict[str, Callable] = {
_type_ingesters: dict[str, Callable] = {
"pymatgen": from_pymatgen,
"ase": from_ase_atoms,
}

_type_ingesters_by_type: Dict[str, Type] = {
_type_ingesters_by_type: dict[str, type] = {
"pymatgen": PymatgenStructure,
"ase": ASEAtoms,
}
4 changes: 2 additions & 2 deletions optimade/adapters/structures/aiida.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This conversion function relies on the [`aiida-core`](https://github.com/aiidateam/aiida-core) package.
"""
from typing import List, Optional
from typing import Optional
from warnings import warn

from optimade.adapters.structures.utils import pad_cell, species_from_species_at_sites
Expand Down Expand Up @@ -48,7 +48,7 @@ def get_aiida_structure_data(optimade_structure: OptimadeStructure) -> Structure
structure = StructureData(cell=lattice_vectors)

# If species not provided, infer data from species_at_sites
species: Optional[List[OptimadeStructureSpecies]] = attributes.species
species: Optional[list[OptimadeStructureSpecies]] = attributes.species
if not species:
species = species_from_species_at_sites(attributes.species_at_sites) # type: ignore[arg-type]

Expand Down
3 changes: 1 addition & 2 deletions optimade/adapters/structures/ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
For more information on the ASE code see [their documentation](https://wiki.fysik.dtu.dk/ase/).
"""
from typing import Dict

from optimade.adapters.exceptions import ConversionError
from optimade.adapters.structures.utils import (
Expand Down Expand Up @@ -66,7 +65,7 @@ def get_ase_atoms(optimade_structure: OptimadeStructure) -> Atoms:
if not species:
species = species_from_species_at_sites(attributes.species_at_sites) # type: ignore[arg-type]

optimade_species: Dict[str, OptimadeStructureSpecies] = {_.name: _ for _ in species}
optimade_species: dict[str, OptimadeStructureSpecies] = {_.name: _ for _ in species}

# Since we've made sure there are no species with more than 1 chemical symbol,
# asking for index 0 will always work.
Expand Down
5 changes: 2 additions & 3 deletions optimade/adapters/structures/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
This conversion function relies on the [NumPy](https://numpy.org/) library.
"""
from typing import Dict

from optimade.adapters.structures.utils import (
cell_to_cellpar,
Expand Down Expand Up @@ -123,11 +122,11 @@ def get_cif( # pylint: disable=too-many-locals,too-many-branches
else:
sites = attributes.cartesian_site_positions

species: Dict[str, OptimadeStructureSpecies] = {
species: dict[str, OptimadeStructureSpecies] = {
species.name: species for species in attributes.species # type: ignore[union-attr]
}

symbol_occurences: Dict[str, int] = {}
symbol_occurences: dict[str, int] = {}
for site_number in range(attributes.nsites): # type: ignore[arg-type]
species_name = attributes.species_at_sites[site_number] # type: ignore[index]
site = sites[site_number]
Expand Down
5 changes: 2 additions & 3 deletions optimade/adapters/structures/proteindatabank.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
Warning:
Currently, the PDBx/mmCIF conversion function is not parsing as a complete PDBx/mmCIF file.
"""
from typing import Dict

try:
import numpy as np
Expand Down Expand Up @@ -164,7 +163,7 @@ def get_pdbx_mmcif( # pylint: disable=too-many-locals
else:
sites = attributes.cartesian_site_positions

species: Dict[str, OptimadeStructureSpecies] = {
species: dict[str, OptimadeStructureSpecies] = {
species.name: species for species in attributes.species # type: ignore[union-attr]
}

Expand Down Expand Up @@ -240,7 +239,7 @@ def get_pdb( # pylint: disable=too-many-locals

pdb += "MODEL 1\n"

species: Dict[str, OptimadeStructureSpecies] = {
species: dict[str, OptimadeStructureSpecies] = {
species.name: species
for species in attributes.species # type:ignore[union-attr]
}
Expand Down
8 changes: 4 additions & 4 deletions optimade/adapters/structures/pymatgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
For more information on the pymatgen code see [their documentation](https://pymatgen.org).
"""
from typing import Dict, List, Optional, Union
from typing import Optional, Union

from optimade.adapters.structures.utils import (
species_from_species_at_sites,
Expand Down Expand Up @@ -105,9 +105,9 @@ def _get_molecule(optimade_structure: OptimadeStructure) -> Molecule:

def _pymatgen_species(
nsites: int,
species: Optional[List[OptimadeStructureSpecies]],
species_at_sites: List[str],
) -> List[Dict[str, float]]:
species: Optional[list[OptimadeStructureSpecies]],
species_at_sites: list[str],
) -> list[dict[str, float]]:
"""
Create list of {"symbol": "concentration"} per site for values to pymatgen species parameters.
Remove vacancies, if they are present.
Expand Down

0 comments on commit a3d1a5f

Please sign in to comment.