Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to identify min_python version #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# pinfo

## Detect minimum version of python

```python
from ppinfo import Project
p = Project(".")
print(p.min_python) # value from your setup.cfg
```
28 changes: 28 additions & 0 deletions src/ppinfo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
"""ppinfo package."""
import configparser
import os
from pathlib import Path
from typing import Union


# pylint: disable=too-few-public-methods
class Project:
"""Class representing a python project."""

# 3.7 default value is used because at the time this library was
# written, this was the oldest still supported version of Python
min_python: str = "3.7"

def __init__(self, path: Union[Path, str] = Path(".")) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a named constructor for the logic part

"""Construct a python Project instance."""
if isinstance(path, str):
path = Path(path)
self.path = path

if (path / "setup.cfg").exists():
config = configparser.ConfigParser()
config.read(path / "setup.cfg")
if "options" in config:
value = config["options"].get("python_requires", None)
if not (value and value.startswith(">=")):
raise NotImplementedError("Unable to parse python_requires")
self.min_python = value[2:]
1 change: 1 addition & 0 deletions test/fixtures/proj1/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty setup.cfg file
2 changes: 2 additions & 0 deletions test/fixtures/py310/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[options]
python_requires = >=3.10
15 changes: 11 additions & 4 deletions test/test_ppinfo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Tests for ppinfo package."""
import pytest

from ppinfo import Project

def test_import() -> None:
"""Test that we can import ppinfo."""
# pylint: disable=unused-import,import-outside-toplevel
import ppinfo

@pytest.mark.parametrize(
("path", "expected_python"),
[("test/fixtures/proj1", "3.7"), ("test/fixtures/py310", "3.10")],
)
def test_min_version(path: str, expected_python: str) -> None:
"""Test ability to find minimal version of python required."""
project = Project(path)
assert project.min_python == expected_python