Skip to content

Commit

Permalink
src/otk/annotation: implement support for python 3.9
Browse files Browse the repository at this point in the history
as we explicitly support python 3.9 and the implementations
of pathlib are different we need to have special support
as we want to inherit from pathlib.
  • Loading branch information
schuellerf committed Oct 18, 2024
1 parent 0553b08 commit 7e9bf68
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/otk/annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copy
import os
import pathlib
from typing import TypeVar, Generic, Any
import sys
from typing import TypeVar, Generic, Any, Optional


class AnnotatedBase:
Expand All @@ -21,7 +22,7 @@ def __init__(self) -> None:
for key, value in self.items():
self[key] = self.deep_convert(value)

def get_annotation(self, key: str, default: Any | None = None) -> Any | None:
def get_annotation(self, key: str, default: Any = None) -> Any:
if key in self._otk_annotations:
return self._otk_annotations[key]
return default
Expand Down Expand Up @@ -176,29 +177,25 @@ def squash_annotations(cls, annotation_list: list) -> dict:


class AnnotatedList(Generic[AnnotatedListT], AnnotatedBase, list):
def __init__(self, seq: list[Any] | None = None) -> None:
def __init__(self, seq: Optional[list[Any]] = None) -> None:
if seq is None:
seq = []
list.__init__(self, seq)
AnnotatedBase.__init__(self)


class AnnotatedDict(Generic[AnnotatedDictKeyT, AnnotatedDictVarT], AnnotatedBase, dict):
def __init__(self, seq: dict[Any, Any] | None = None, **kwargs: dict[Any, Any]) -> None:
def __init__(self, seq: Optional[dict[Any, Any]] = None, **kwargs: dict[Any, Any]) -> None:
if seq is None:
seq = {}
dict.__init__(self, seq, **kwargs)
AnnotatedBase.__init__(self)


class AnnotatedPath(AnnotatedBase, pathlib.Path):
def __init__(self, *args: Any, **kwargs: Any) -> None:
pathlib.Path.__init__(self, *args, **kwargs)
AnnotatedBase.__init__(self)

class AnnotatedPathBase(AnnotatedBase):
def fspath_with_include(self) -> str:
src = None
filename = os.path.relpath(os.fspath(self), self._basedir)
filename = os.path.relpath(os.fspath(str(self)), self._basedir)
try:
src = self.get_annotation("src")
except KeyError:
Expand All @@ -208,6 +205,18 @@ def fspath_with_include(self) -> str:
return filename


if sys.version_info >= (3, 10):
class AnnotatedPath(AnnotatedPathBase, pathlib.Path):
def __init__(self, *args: Any, **kwargs: Any) -> None:
pathlib.Path.__init__(self, *args, **kwargs)
AnnotatedPathBase.__init__(self)
else:
class AnnotatedPath(AnnotatedPathBase, pathlib.PosixPath): # pylint: disable=abstract-method
def __init__(self, *args: Any) -> None: # pylint: disable=W0613
pathlib.PosixPath.__init__(self)
AnnotatedPathBase.__init__(self)


class AnnotatedStr(AnnotatedBase, str):
def __init__(self, *args: str) -> None: # pylint: disable=W0613
str.__init__(self)
Expand Down

0 comments on commit 7e9bf68

Please sign in to comment.