Skip to content

Commit

Permalink
Python version safed import of TypeAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Sep 4, 2024
1 parent ea7b449 commit e68db9c
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions extras/fileformats/extras/application/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pydra.engine.specs
from fileformats.generic import FsObject
from fileformats.core.utils import set_cwd
from fileformats.core.type_aliases import PathType
from fileformats.core.typing import PathType
from fileformats.core import converter, FileSet
from fileformats.application import Zip, Tar, TarGzip

Expand Down Expand Up @@ -238,4 +238,4 @@ def relative_path(path: PathType, base_dir: PathType) -> str:
f"Cannot add {path} to archive as it is not a "
f"subdirectory of {base_dir}"
)
return relpath
return relpath # type: ignore[no-any-return]
2 changes: 1 addition & 1 deletion extras/fileformats/extras/application/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def convert_data_serialization(
output_path = out_dir / (
in_file.fspath.stem + (output_format.ext if output_format.ext else "")
)
return output_format.save_new(output_path, dct)
return output_format.save_new(output_path, dct) # type: ignore[no-any-return]


@extra_implementation(DataSerialization.load)
Expand Down
2 changes: 1 addition & 1 deletion extras/fileformats/extras/image/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def convert_image(
output_path = out_dir / (
in_file.fspath.stem + (output_format.ext if output_format.ext else "")
)
return output_format.save_new(output_path, data_array)
return output_format.save_new(output_path, data_array) # type: ignore[no-any-return]
2 changes: 1 addition & 1 deletion fileformats/application/serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import typing as ty
from typing_extensions import Self, TypeAlias
from fileformats.core.typing import Self, TypeAlias
from pathlib import Path
from fileformats.core import extra, DataType, FileSet, extra_implementation
from fileformats.core.mixin import WithClassifiers
Expand Down
4 changes: 2 additions & 2 deletions fileformats/core/datatype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from inspect import isclass
import typing as ty
from typing_extensions import Self
from fileformats.core.typing import Self
from abc import ABCMeta
import importlib
import itertools
Expand All @@ -27,7 +27,7 @@
class DataType(Classifier, metaclass=ABCMeta):
is_fileset = False
is_field = False
nested_types: ty.Tuple[ty.Type["DataType"], ...] = ()
nested_types: ty.Tuple[ty.Type[Classifier], ...] = ()
# Store converters registered by @converter decorator that convert to FileSet
# NB: each class will have its own version of this dictionary
converters: ty.Dict[
Expand Down
2 changes: 1 addition & 1 deletion fileformats/core/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import functools
import urllib.error
import fileformats.core
from typing_extensions import TypeAlias
from fileformats.core.typing import TypeAlias
from .datatype import DataType
from .converter_helpers import ConverterWrapper, ConverterSpec, SubtypeVar
from .exceptions import FormatConversionError, FileFormatsExtrasError
Expand Down
4 changes: 2 additions & 2 deletions fileformats/core/fileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
from pathlib import Path
import hashlib
import logging
from typing_extensions import Self
from fileformats.core.typing import Self
from .utils import (
classproperty,
fspaths_converter,
describe_task,
matching_source,
import_extras_module,
)
from .type_aliases import FspathsInputType, CryptoMethod, PathType
from .typing import FspathsInputType, CryptoMethod, PathType
from .sampling import SampleFileGenerator
from .identification import (
to_mime_format_name,
Expand Down
6 changes: 3 additions & 3 deletions fileformats/core/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class MyFileFormatWithSeparateHeader(WithSeparateHeader, MyFileFormat):
header_type: ty.Type["fileformats.core.FileSet"]

@classproperty
def nested_types(cls) -> ty.Tuple[ty.Type["DataType"], ...]:
def nested_types(cls) -> ty.Tuple[ty.Type[Classifier], ...]:
return (cls.header_type,)

@property
Expand Down Expand Up @@ -235,7 +235,7 @@ def read_metadata(
return metadata

@classproperty
def nested_types(cls) -> ty.Tuple[ty.Type["DataType"], ...]:
def nested_types(cls) -> ty.Tuple[ty.Type[Classifier], ...]:
return cls.side_car_types


Expand Down Expand Up @@ -316,7 +316,7 @@ def is_classified(cls) -> bool:
return "unclassified" in cls.__dict__

@classproperty
def nested_types(cls) -> ty.Tuple[ty.Type["DataType"], ...]:
def nested_types(cls) -> ty.Tuple[ty.Type[Classifier], ...]:
return cls.classifiers

@classmethod
Expand Down
10 changes: 9 additions & 1 deletion fileformats/core/type_aliases.py → fileformats/core/typing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import typing as ty
import sys
from pathlib import Path
from typing_extensions import TypeAlias

if sys.version_info[:2] < (3, 11):
from typing_extensions import TypeAlias, Self
else:
from typing import TypeAlias, Self

if ty.TYPE_CHECKING:
import fileformats.core

Check warning on line 11 in fileformats/core/typing.py

View check run for this annotation

Codecov / codecov/patch

fileformats/core/typing.py#L11

Added line #L11 was not covered by tests
Expand All @@ -15,3 +20,6 @@
]

PathType: TypeAlias = ty.Union[str, Path]


__all__ = ["CryptoMethod", "FspathsInputType", "PathType", "TypeAlias", "Self"]
2 changes: 1 addition & 1 deletion fileformats/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import pkgutil
from contextlib import contextmanager
from .type_aliases import FspathsInputType
from .typing import FspathsInputType
import fileformats.core

if ty.TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion fileformats/generic/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .fsobject import FsObject
from fileformats.core.fileset import FileSet, FILE_CHUNK_LEN_DEFAULT
from fileformats.core.mixin import WithClassifiers
from fileformats.core.type_aliases import CryptoMethod
from fileformats.core.typing import CryptoMethod


class Directory(FsObject):
Expand Down
2 changes: 1 addition & 1 deletion fileformats/generic/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def actual_ext(self) -> str:
"(i.e. matches the None extension)"
)
# Return the longest matching extension, useful for optional extensions
return sorted(matching, key=len)[-1]
return sorted(matching, key=len)[-1] # type: ignore[no-any-return]

@property
def stem(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion fileformats/image/raster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing_extensions import Self, TypeAlias
from fileformats.core.typing import Self, TypeAlias
import typing as ty
from fileformats.core.mixin import WithMagicNumber
from fileformats.core import extra
Expand Down

0 comments on commit e68db9c

Please sign in to comment.