Skip to content

Commit

Permalink
Merge pull request #50 from sbrunner/toml
Browse files Browse the repository at this point in the history
Use tomllib from Python standard lib, for version that provides it
  • Loading branch information
carlio authored Nov 8, 2024
2 parents 7c40735 + 60147a9 commit 6921ee3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ detect-requirements = 'requirements_detector.run:run'
python = ">=3.8,<4.0"
astroid = "^3.0"
packaging = ">=21.3"
toml = "^0.10.2"
toml = {version = "^0.10.2", python = "<3.11"}
semver = "^3.0.0"

[tool.poetry.dev-dependencies]
Expand Down
13 changes: 12 additions & 1 deletion requirements_detector/detect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import sys
from pathlib import Path
from typing import List, Union

Expand All @@ -9,6 +10,12 @@
from .poetry_semver import parse_constraint
from .requirement import DetectedRequirement

_USE_TOMLLIB = sys.version_info.major > 3 or sys.version_info.minor >= 11
if _USE_TOMLLIB:
import tomllib
else:
import toml

__all__ = [
"find_requirements",
"from_requirements_txt",
Expand Down Expand Up @@ -108,7 +115,11 @@ def from_pyproject_toml(toml_file: P) -> List[DetectedRequirement]:
if isinstance(toml_file, str):
toml_file = Path(toml_file)

parsed = toml.load(toml_file)
if _USE_TOMLLIB:
with open(toml_file, "rb") as toml_file_open:
parsed = tomllib.load(toml_file_open)
else:
parsed = toml.load(toml_file)
poetry_section = parsed.get("tool", {}).get("poetry", {})
dependencies = poetry_section.get("dependencies", {})
dependencies.update(poetry_section.get("dev-dependencies", {}))
Expand Down

0 comments on commit 6921ee3

Please sign in to comment.