Skip to content

Commit

Permalink
PD-67 set python 3.11 as min version (#16)
Browse files Browse the repository at this point in the history
* Add python version checks

* fix unit tests

* update unit tests for rmarkdown to track only 100%

* fix last workflow unit test

* fix the correct test to show only 100% progress

* finally fix the tests...

* bump version
  • Loading branch information
igor-sb authored May 6, 2024
1 parent 8cacd55 commit 47d3873
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ sphinx:
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
- requirements: docs/requirements.txt
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ enforcement ladder](https://github.com/mozilla/diversity).

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
https://www.contributor-covenant.org/translations.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Contributing code guidelines

Thank you for contributing!

Igor
Igor
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"name": "Switch to light mode",
},
},
],
],
# Visible levels of the global TOC; -1 means unlimited
# 'globaltoc_depth': 1,
# If False, expand all TOC entries
Expand Down Expand Up @@ -96,7 +96,7 @@
"link": "https://pypi.org/project/sphinx-immaterial/",
},
],
# END: social icons
# END: social icons
}

html_sidebars = {
Expand Down
4 changes: 2 additions & 2 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data.yaml
│ ├── preprocessed
│ │ └── user_data.csv
│ ├── final
│ │ └── user_data.csv
│ │ └── user_data.csv
│ └── metadata.csv
...
Expand All @@ -75,7 +75,7 @@ Once loaded with ``load_data_yaml()`` from ``bluprint_conf``, these relative pat
final: raw/user_data.csv
metadata: metadata.csv
internal_binary: /absolute/path/to/local_binary
report: s3://path/to/final_report.ipynb
report: s3://path/to/final_report.ipynb
.. _config-workflows:

Expand Down
4 changes: 2 additions & 2 deletions docs/source/prod_notebooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ To keep cell output under version control, add ``--keep-output`` argument to *.g
[diff "ipynb"]
textconv = \"/path/to/project/.venv/bin/python\" -m nbstripout --keep-output -t
For more details see nbstripout `instructions <https://github.com/kynan/nbstripout>`_.
For more details see nbstripout `instructions <https://github.com/kynan/nbstripout>`_.


Best coding practices
Expand Down Expand Up @@ -122,4 +122,4 @@ Notebook workflows can be tested by specifying yaml workflow file, e.g. ``workfl
--workflow_yaml=example_workflow \
--notebook_dir=notebooks
For Jupyter notebook workflows with more features, check `ploomber <https://docs.ploomber.io/en/latest/get-started/what-is.html>`_.
For Jupyter notebook workflows with more features, check `ploomber <https://docs.ploomber.io/en/latest/get-started/what-is.html>`_.
2 changes: 1 addition & 1 deletion docs/source/prod_projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bluprint projects can be shared and reused directly out-of-the-box. Simply copy

.. note:
Existing bluprint projects can be run without ``bluprint``.
Existing bluprint projects can be run without ``bluprint``.
For example, to download and test the demo project:

Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "bluprint"
version = "0.1.4"
version = "0.1.5"
description = ""
authors = [
{name = "igor-sb"},
Expand Down Expand Up @@ -52,11 +52,12 @@ filterwarnings = [

[tool.isort]
profile = "wemake"
line_length = 79
line_length = 80

[tool.flake8]
max-module-members = 8
max-arguments = 6
max-line-length = 80
ignore = [
'D101', # D101 Missing docstring in public class
'D102', # D102 Missing docstring in public method
Expand Down
42 changes: 30 additions & 12 deletions src/bluprint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

from bluprint.binary import check_if_executable_is_installed
from bluprint.colors import styled_print
from bluprint.create.py_project import create_project, initialize_project
from bluprint.create.py_project import (
check_python_version,
create_project,
initialize_project,
)
from bluprint.create.r_project import (
check_if_r_package_is_installed,
initialize_r_project,
Expand Down Expand Up @@ -54,8 +58,9 @@ def create(
project directory.
python_version (str | None, optional): Python version to be used. If
not specified, uses the latest stable version from
`pyenv install -l`.
not specified, uses either the latest stable version from
`pyenv install -l` or 3.11 (whichever is greater). Bluprint requires
Python >= 3.11.
parent_dir (str | None, optional): Parent directory to create a
PROJECT_NAME directory in. If not specific PARENT_DIR is a current
Expand All @@ -77,6 +82,7 @@ def create(
endline='',
)
check_if_project_can_be_created(project_name, parent_dir, r_proj)
check_python_version(python_version)
create_project(project_name, python_version, parent_dir, template_dir)
if r_proj:
initialize_r_project(project_name, parent_dir)
Expand All @@ -95,20 +101,27 @@ def init(
Args:
project_name (str): Name of the project.
python_version (str | None, optional): Python version to be used.
If not specified, uses `python --version`.
python_version (str | None, optional): Python version to be used. If
not specified, uses either the latest stable version from
`pyenv install -l` or 3.11 (whichever is greater). Bluprint requires
Python >= 3.11.
project_dir (str | None, optional): Project directory where to
initialize a new bluprint project. By default uses current working
directory.
template_dir (str | None, optional): Path to a directory with a
Bluprint or PDM template. If not specified (default), uses Bluprint
default built-in template.
r_proj (bool): Setup R library using renv to support package
isolation in RMarkdown notebooks.
Raises:
ProjectExistsError: Raised if pyproject.toml exists in the
`project_dir`.
ProjectExistsError: Raised if pyproject.toml exists in
the `project_dir`.
"""
if not project_dir:
project_dir = getcwd()
Expand All @@ -119,11 +132,8 @@ def init(
),
endline='',
)
if (Path(project_dir) / 'pyproject.toml').exists():
raise ProjectExistsError(
f'pyproject.toml already exists in {project_dir}: '
+ 'cannot initialize new bluprint project',
)
check_if_pyproject_toml_exists(project_dir)
check_python_version(python_version)
initialize_project(
project_name,
python_version,
Expand Down Expand Up @@ -225,6 +235,14 @@ def check_if_project_exists(project_name: str, parent_dir: str | None) -> None:
raise ProjectExistsError(f'{project_name} directory exists.')


def check_if_pyproject_toml_exists(project_dir: str) -> None:
if (Path(project_dir) / 'pyproject.toml').exists():
raise ProjectExistsError(
f'pyproject.toml already exists in {project_dir}: '
+ 'cannot initialize new bluprint project',
)


def main():
fire.Fire(Bluprint)

Expand Down
4 changes: 4 additions & 0 deletions src/bluprint/create/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class PythonVersionError(StyledError):
"""Raises exception if python --version fails."""


class LowPythonVersionError(StyledError):
"""Raises exception if user-provided Python version is < 3.11."""


class PdmAddError(StyledError):
"""Raises exception if PDM add fails."""

Expand Down
24 changes: 21 additions & 3 deletions src/bluprint/create/py_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

import nbformat
from importlib_resources import files
from packaging.version import Version

from bluprint.binary import pdm_add, pdm_init, run
from bluprint.create.errors import GitError, PythonVersionError
from bluprint.create.errors import (
GitError,
LowPythonVersionError,
PythonVersionError,
)

MIN_PYTHON_VERSION = '3.11'


def create_project(
Expand Down Expand Up @@ -90,6 +97,17 @@ def replace_git_account_name(
readme_w.write(readme_content)


def default_python_version() -> str:
def default_python_version(min_version: str = MIN_PYTHON_VERSION) -> str:
python_out = run(['python', '--version'], PythonVersionError)
return python_out.strip().replace('Python ', '')
py_version = python_out.strip().replace('Python ', '')
if Version(py_version) >= Version(min_version):
return py_version
return min_version


def check_python_version(
python_version: str | None,
) -> None:
if python_version:
if Version(python_version) < Version(MIN_PYTHON_VERSION):
raise LowPythonVersionError('Bluprint requires Python >= 3.11.')
1 change: 1 addition & 0 deletions src/bluprint/notebook/rmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def run_rmarkdown_notebook( # noqa: WPS210
stderr=subprocess.PIPE,
)
last_percent = 0
current_percent = 0
for line in rmd_out.stdout: # type: ignore
if progress_bar_pct.match(line):
current_percent = int(progress_bar_pct.sub(r'\1', line))
Expand Down
2 changes: 1 addition & 1 deletion src/bluprint/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pdm install
Open and run a notebook in your IDE of choice (VSCode, Jupyter, ...). Make sure
to select the correct kernel, which should point to the `.venv` virtual
environment inside this directory. In VSCode it will show as `.venv/bin/python`
and should be marked as "Recommended".
and should be marked as "Recommended".
2 changes: 1 addition & 1 deletion src/bluprint/template/notebooks/example_rmarkdown.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ data
```{r}
df <- read.table(data["example"], sep = ",", header = TRUE)
df
```
```
9 changes: 9 additions & 0 deletions tests/create/test_create_py.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Test creating a new Python project."""

from pathlib import Path

import pytest
from importlib_resources import files

from bluprint import cli
from bluprint.create.errors import LowPythonVersionError


def test_create_py_project(find_files_in_dir, tmp_path):
Expand Down Expand Up @@ -33,3 +37,8 @@ def test_create_py_project(find_files_in_dir, tmp_path):
assert (venv_dir / 'lib').exists()
assert (venv_dir / 'share').exists()
assert (venv_dir / 'pyvenv.cfg').exists()


def test_low_python_version():
with pytest.raises(LowPythonVersionError):
cli.check_python_version('3.10')
2 changes: 1 addition & 1 deletion tests/workflow/fixtures/rtest/test5.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Sys.sleep(0.1)
```{r}
print('And again!')
Sys.sleep(0.1)
```
```
Loading

0 comments on commit 47d3873

Please sign in to comment.