Skip to content

Commit

Permalink
Merge pull request #1343 from dandi/better-types
Browse files Browse the repository at this point in the history
Modernize type annotations and improve mypy config
  • Loading branch information
yarikoptic authored Nov 8, 2023
2 parents 1f68cee + f23cfff commit b6717fb
Show file tree
Hide file tree
Showing 48 changed files with 575 additions and 529 deletions.
2 changes: 1 addition & 1 deletion dandi/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# A trick found on https://github.com/h5py/h5py/issues/1079#issuecomment-567081386
# to avoid some weird behavior on Yarik's laptop where MPI fails to initialize
# and that takes h5py additional 5 seconds to import
import mpi4py
import mpi4py # type: ignore[import-not-found]

mpi4py.rc(initialize=False)
except Exception:
Expand Down
6 changes: 2 additions & 4 deletions dandi/cli/cmd_move.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from typing import Optional

import click

from .base import devel_debug_option, instance_option, map_to_click_exceptions
Expand Down Expand Up @@ -47,10 +45,10 @@
@map_to_click_exceptions
def move(
paths: tuple[str],
dandiset: Optional[str],
dandiset: str | None,
dry_run: bool,
existing: str,
jobs: Optional[int],
jobs: int | None,
regex: bool,
work_on: str,
dandi_instance: str,
Expand Down
16 changes: 8 additions & 8 deletions dandi/cli/cmd_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import os
import re
from typing import List, Optional, cast
from typing import cast
import warnings

import click
Expand Down Expand Up @@ -90,10 +90,10 @@ def validate_bids(
@map_to_click_exceptions
def validate(
paths: tuple[str, ...],
ignore: Optional[str],
ignore: str | None,
grouping: str,
min_severity: str,
schema: Optional[str] = None,
schema: str | None = None,
devel_debug: bool = False,
allow_any_path: bool = False,
) -> None:
Expand Down Expand Up @@ -139,7 +139,7 @@ def validate(
def _process_issues(
validator_result: Iterable[ValidationResult],
grouping: str,
ignore: Optional[str] = None,
ignore: str | None = None,
) -> None:
issues = [i for i in validator_result if i.severity is not None]
if ignore is not None:
Expand All @@ -149,7 +149,7 @@ def _process_issues(
display_errors(
purviews,
[i.id for i in issues],
cast(List[Severity], [i.severity for i in issues]),
cast("list[Severity]", [i.severity for i in issues]),
[i.message for i in issues],
)
elif grouping == "path":
Expand All @@ -161,7 +161,7 @@ def _process_issues(
display_errors(
[purview],
[i.id for i in applies_to],
cast(List[Severity], [i.severity for i in applies_to]),
cast("list[Severity]", [i.severity for i in applies_to]),
[i.message for i in applies_to],
)
else:
Expand All @@ -185,10 +185,10 @@ def _get_severity_color(severities: list[Severity]) -> str:


def display_errors(
purviews: list[Optional[str]],
purviews: list[str | None],
errors: list[str],
severities: list[Severity],
messages: list[Optional[str]],
messages: list[str | None],
) -> None:
"""
Unified error display for validation CLI, which auto-resolves grouping
Expand Down
2 changes: 1 addition & 1 deletion dandi/cli/tests/test_cmd_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from dandi.tests.skip import mark

from ..command import ls
from ..cmd_ls import ls
from ...utils import yaml_load


Expand Down
4 changes: 3 additions & 1 deletion dandi/cli/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from click.testing import CliRunner
import pytest

from ..command import __all_commands__, ls, validate
from ..cmd_ls import ls
from ..cmd_validate import validate
from ..command import __all_commands__


@pytest.mark.parametrize("command", (ls, validate))
Expand Down
2 changes: 1 addition & 1 deletion dandi/cli/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from click.testing import CliRunner
import pytest

from ..command import download
from ..cmd_download import download
from ...consts import dandiset_metadata_file, known_instances


Expand Down
2 changes: 1 addition & 1 deletion dandi/cli/tests/test_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from click.testing import CliRunner

from ..command import instances
from ..cmd_instances import instances


def test_cmd_instances(monkeypatch):
Expand Down
2 changes: 1 addition & 1 deletion dandi/cli/tests/test_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from pytest_mock import MockerFixture

from ..command import move
from ..cmd_move import move


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion dandi/cli/tests/test_service_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dandi import __version__
from dandi.tests.fixtures import SampleDandiset

from ..command import service_scripts
from ..cmd_service_scripts import service_scripts

DATA_DIR = Path(__file__).with_name("data")

Expand Down
4 changes: 2 additions & 2 deletions dandi/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from __future__ import annotations

from _pytest.config import Config
from _pytest.config.argparsing import Parser
Expand All @@ -16,7 +16,7 @@ def pytest_addoption(parser: Parser) -> None:
)


def pytest_collection_modifyitems(items: List[Item], config: Config) -> None:
def pytest_collection_modifyitems(items: list[Item], config: Config) -> None:
# Based on <https://pythontesting.net/framework/pytest/pytest-run-tests
# -using-particular-fixture/>
if config.getoption("--dandi-api"):
Expand Down
6 changes: 4 additions & 2 deletions dandi/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass
from enum import Enum
import os
from typing import Optional
from typing import Union

#: A list of metadata fields which dandi extracts from .nwb files.
#: Additional fields (such as ``number_of_*``) might be added by
Expand Down Expand Up @@ -99,7 +99,9 @@ class EmbargoStatus(Enum):
@dataclass(frozen=True)
class DandiInstance:
name: str
gui: Optional[str]
# This class is used as an attribute of ParsedDandiURL, a Pydantic class,
# and thus we need non-future annotations:
gui: Union[str, None]
api: str

@property
Expand Down
Loading

0 comments on commit b6717fb

Please sign in to comment.