Skip to content

Commit

Permalink
feat: Add support for python 3.9+
Browse files Browse the repository at this point in the history
feat: Add support for python 3.9+
  • Loading branch information
kvankova authored Nov 24, 2024
2 parents 8bcbfa0 + db3689b commit 142c320
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 92 deletions.
6 changes: 4 additions & 2 deletions code_embedder/code_embedding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from loguru import logger

from code_embedder.script_content_reader import ScriptContentReaderInterface
Expand All @@ -9,7 +11,7 @@ class CodeEmbedder:
def __init__(
self,
readme_paths: list[str],
changed_files: list[str] | None,
changed_files: Optional[list[str]],
script_metadata_extractor: ScriptMetadataExtractorInterface,
script_content_reader: ScriptContentReaderInterface,
) -> None:
Expand Down Expand Up @@ -56,7 +58,7 @@ def _read_readme(self, readme_path: str) -> list[str]:

def _extract_scripts(
self, readme_content: list[str], readme_path: str
) -> list[ScriptMetadata] | None:
) -> Optional[list[ScriptMetadata]]:
scripts = self._script_metadata_extractor.extract(readme_content=readme_content)
if not scripts:
logger.debug(f"No script paths found in README in path {readme_path}. Skipping.")
Expand Down
10 changes: 6 additions & 4 deletions code_embedder/script_content_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ast
import re
from typing import Protocol
from typing import Optional, Protocol

from code_embedder.script_metadata import ScriptMetadata

Expand Down Expand Up @@ -83,7 +83,9 @@ def _extract_part(self, script: ScriptMetadata) -> str:

return "\n".join(lines[start:end])

def _extract_object_part(self, script: ScriptMetadata) -> tuple[int | None, int | None]:
def _extract_object_part(
self, script: ScriptMetadata
) -> tuple[Optional[int], Optional[int]]:
tree = ast.parse(script.content)

for node in ast.walk(tree):
Expand All @@ -100,8 +102,8 @@ def _extract_object_part(self, script: ScriptMetadata) -> tuple[int | None, int
return None, None

def _extract_section_part(
self, lines: list[str], section: str | None = None
) -> tuple[int | None, int | None]:
self, lines: list[str], section: Optional[str] = None
) -> tuple[Optional[int], Optional[int]]:
if not section:
return None, None

Expand Down
4 changes: 2 additions & 2 deletions code_embedder/script_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Literal
from typing import Literal, Optional


@dataclass
Expand All @@ -8,5 +8,5 @@ class ScriptMetadata:
readme_end: int
path: str
extraction_type: Literal["section", "object", "full"] = "full"
extraction_part: str | None = None
extraction_part: Optional[str] = None
content: str = ""
4 changes: 2 additions & 2 deletions code_embedder/script_metadata_extractor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Protocol
from typing import Optional, Protocol

from code_embedder.script_metadata import ScriptMetadata

Expand Down Expand Up @@ -40,7 +40,7 @@ def _is_code_block_start(self, line: str) -> bool:
def _is_code_block_end(self, line: str) -> bool:
return line.strip() == self._code_block_end

def _start_new_block(self, line: str, row: int) -> dict | None:
def _start_new_block(self, line: str, row: int) -> Optional[dict]:
tag_items = line.split(self._path_separator)
path = tag_items[1].strip()
extraction_type = tag_items[2].strip() if len(tag_items) > 2 else "full"
Expand Down
221 changes: 146 additions & 75 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT"
code-embedder = "code_embedder.main:app"

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.9"
loguru = "^0.7.2"
typer-slim = "^0.13.0"

Expand All @@ -27,5 +27,5 @@ build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 95
lint.select = ["E", "F", "I", "BLE", "UP", "FA"]
target-version = "py311"
lint.select = ["E", "F", "I", "BLE", "UP"]
target-version = "py39"
4 changes: 2 additions & 2 deletions tests/test_script_content_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal
from typing import Literal, Optional

import pytest

Expand All @@ -9,7 +9,7 @@
def create_script_metadata(
path: str,
extraction_type: Literal["full", "section", "object"] = "full",
extraction_part: str | None = None,
extraction_part: Optional[str] = None,
content: str = "",
) -> ScriptMetadata:
return ScriptMetadata(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_script_metadata_extractor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal
from typing import Literal, Optional

import pytest

Expand All @@ -11,7 +11,7 @@ def create_script_metadata(
readme_end: int,
path: str,
extraction_type: Literal["section", "object", "full"] = "full",
extraction_part: str | None = None,
extraction_part: Optional[str] = None,
content: str = "",
) -> ScriptMetadata:
return ScriptMetadata(
Expand Down

0 comments on commit 142c320

Please sign in to comment.