From 5d357d878bec9fbd9b64c68fa994bd5f34cc0287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 17 Oct 2024 14:41:52 +0200 Subject: [PATCH] Use tomllib for Python that provide him --- poetry.lock | 4 ++-- pyproject.toml | 2 +- requirements_detector/detect.py | 13 ++++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/poetry.lock b/poetry.lock index 628b7ad..9be7b53 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "astroid" @@ -1091,4 +1091,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.8,<4.0" -content-hash = "043b4984ec0f2a53aa6136ccaf7276a61590b6b4354d6d00a2016290c401f0ba" +content-hash = "e66f7c6b94e9d01a76d3a1a15be1bfe3de124034a530e4d771d19f808c78e854" diff --git a/pyproject.toml b/pyproject.toml index 5b9224f..437d94e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/requirements_detector/detect.py b/requirements_detector/detect.py index a684817..d3f801f 100644 --- a/requirements_detector/detect.py +++ b/requirements_detector/detect.py @@ -1,4 +1,5 @@ import re +import sys from pathlib import Path from typing import List, Union @@ -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", @@ -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) 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", {}))