Skip to content

Commit

Permalink
Install typing-extensions for Py3.10 and below
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Oct 9, 2024
1 parent ac74965 commit 54dd165
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ zip_safe = False
# bin/second.py

install_requires =
# snazzy
typing-extensions;python_version<'3.11'

# [options.package_data]
# * = *.txt, *.rst
Expand Down
59 changes: 33 additions & 26 deletions tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@


class TestFS:
# @pytest.mark.skipif(os.name == "nt", reason="windows has different eol size")
# def test_fs_linux(self):
# path = Path(__file__).parent / "fixtures"

# # We check for unix line endings/file sizes (as used on travis)
# tree = load_tree_from_fs(path)
# assert fixture.check_content(
# tree,
# """
# FileSystemTree<*>
# ├── 'file_1.txt', 13 bytes, 2022-04-14 21:35:21
# ╰── [folder_1]
# ╰── 'file_1_1.txt', 15 bytes, 2022-04-14 21:35:21
# """,
# )

# tree = load_tree_from_fs(path, sort=False)
# assert "[folder_1]" in fixture.canonical_repr(tree)

# @pytest.mark.skipif(os.name != "nt", reason="windows has different eol size")
# def test_fs_windows(self):
# path = Path(__file__).parent / "fixtures"
# # Cheap test only,
# tree = load_tree_from_fs(path)
# assert "[folder_1]" in fixture.canonical_repr(tree)

def test_fs_serialize(self):
KEEP_FILES = False
path = Path(__file__).parent / "fixtures"
Expand Down Expand Up @@ -69,3 +43,36 @@ def test_fs_serialize_unsorted(self):
tree = load_tree_from_fs(path, sort=False)
assert "[folder_1]" in fixture.canonical_repr(tree)
assert len(tree) == 3

# NOTE: these tests are not very useful, since they depend on the file system
# and the file system is not under our control (e.g. line endings, file sizes, etc.)
# Especially on GitHub Actions we have no control over the file system and
# timestamps, so we cannot compare the output of `load_tree_from_fs` with a
# fixture.
# We should only test the serialization/deserialization here.

# @pytest.mark.skipif(os.name == "nt", reason="windows has different eol size")
# def test_fs_linux(self):
# path = Path(__file__).parent / "fixtures"

# # We check for unix line endings/file sizes (as used on travis)
# tree = load_tree_from_fs(path)
# assert fixture.check_content(
# tree,
# """
# FileSystemTree<*>
# ├── 'file_1.txt', 13 bytes, 2022-04-14 21:35:21
# ╰── [folder_1]
# ╰── 'file_1_1.txt', 15 bytes, 2022-04-14 21:35:21
# """,
# )

# tree = load_tree_from_fs(path, sort=False)
# assert "[folder_1]" in fixture.canonical_repr(tree)

# @pytest.mark.skipif(os.name != "nt", reason="windows has different eol size")
# def test_fs_windows(self):
# path = Path(__file__).parent / "fixtures"
# # Cheap test only,
# tree = load_tree_from_fs(path)
# assert "[folder_1]" in fixture.canonical_repr(tree)
39 changes: 20 additions & 19 deletions tests/test_typing_concept.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@

from __future__ import annotations

import pytest

# pytest.skip(allow_module_level=True)
from typing import Any, Generic, List, Type, TypeVar, cast

try:
from typing import Any, Generic, List, Self, TypeVar, cast
from typing import Self
except ImportError as e:
print(f"ImportError: {e}")
from typing_extensions import Self
# _ = pytest.importorskip("typing_extensions")
# from typing_extensions import Self
typing_extensions = pytest.importorskip("typing_extensions")
Self = typing_extensions.Self


# TData = TypeVar("TData")
TNode = TypeVar("TNode", bound="Node")


class Node:
def __init__(self, data: Any, parent: Node):
class Node(Generic[TNode]):
def __init__(self, data: Any, parent: Self):
self.data: Any = data
self.parent: Node = parent
self.children: List[Node] = []
self.parent: Self = parent
self.children: List[Self] = []

def add(self, data: Any) -> Node:
node = Node(data, self)
def add(self, data: Any) -> Self:
node = self.__class__(data, self)
self.children.append(node)
return node


class Tree(Generic[TNode]):
node_class: Type[TNode] = Node

def __init__(self):
self.root: Node = Node("__root__", cast(TNode, None))
self.root: TNode = self.node_class("__root__", cast(TNode, None))

def add(self, data: Any) -> Node:
def add(self, data: Any) -> TNode:
node = self.root.add(data)
return node

Expand All @@ -44,26 +44,27 @@ def first(self) -> TNode:


# ----------------------------
# ----------------------------


class TypedNode(Node):
def __init__(self, data: Any, kind: str, parent: TypedNode):
def __init__(self, data: Any, kind: str, parent: Self):
super().__init__(data, parent)
self.kind: str = kind
# self.children: List[TypedNode] = []

def add(self, data: Any, kind: str) -> TypedNode:
node = TypedNode(data, kind, self)
def add(self, data: Any, kind: str) -> Self:
node = self.__class__(data, kind, self)
self.children.append(node)
return node


class TypedTree(Tree[TypedNode]):
node_class = TypedNode

def __init__(self):
self.root: TypedNode = TypedNode("__root__", "__root__", cast(TypedNode, None))

def add(self, data: Any, kind: str) -> TNode:
def add(self, data: Any, kind: str) -> TypedNode:
node = self.root.add(data, kind)
return node

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ deps =
pytest-cov
pytest-html
rdflib
# py39: typing-extensions>=4.0
# py310: typing-extensions>=4.0
setenv =
COVERAGE_FILE=.coverage.{envname}
; allowlist_externals =
Expand Down

0 comments on commit 54dd165

Please sign in to comment.