Skip to content

Commit

Permalink
feat: throw on typegen with no registered namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBRas committed Jan 7, 2024
1 parent c2b56dd commit d5f661b
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 213 deletions.
209 changes: 105 additions & 104 deletions polugins/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion polugins/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tomli = ">=0.2.0"

[tool.poetry.group.dev.dependencies]
example-package = {path = "tests/pkgs/example_package"}
ruff = "0.1.6"
ruff = "0.1.11"
nox = "^2023.4.22"
pytest = "^7.3.1"
pytest-env = "^0.8.2"
Expand Down
226 changes: 122 additions & 104 deletions polugins_type_gen/poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion polugins_type_gen/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ optional = true
polugins = ">=0.2" # We use it for the scripts/generate_polars_stubs.py
pytest = "^7.3.1"
mypy = "1.7.1" # Not sure it's considered Public API what we import, so we fix the version.
ruff = "0.1.6"
ruff = "0.1.11"
nox = "^2023.4.22"
coverage = "^7.2.7"
pytest-env = "^0.8.2"
pytest-cov = "^4.1.0"
example-package = {path = "tests/pkgs/example_package"}
pytest-mock = "^3.12.0"

[tool.poetry.group.nox]
optional = true
Expand Down
18 changes: 17 additions & 1 deletion polugins_type_gen/src/polugins_type_gen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from polugins.main import _get_namespaces


class MissingVersionException(Exception):
pass


class NoNamespaceRegisteredException(Exception):
pass


def has_version(version: str) -> bool:
files = importlib_resources.files("polugins_type_gen")
return (files / "_stubs" / version).is_dir()
Expand All @@ -19,10 +27,18 @@ def create_stubs(version: str):
" This is usually because the version has been yanked or because it's new."
" Feel free to create an issue if you want types for this version."
)
raise ValueError(msg)
raise MissingVersionException(msg)

all_namespaces = _get_namespaces()

if all(namespace == {} for namespace in all_namespaces.values()):
msg = (
"No namespaces found. No types will be generated as this is usually an error."
" Note that only namespaces registered through config, env vars or endpoints"
" can have types genered for them."
)
raise NoNamespaceRegisteredException(msg)

for extension_class, namespaces in all_namespaces.items():
if namespaces:
files = importlib_resources.files("polugins_type_gen")
Expand Down
27 changes: 25 additions & 2 deletions polugins_type_gen/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import pytest
from polugins._types import ExtensionClass

from polugins_type_gen.cli import create_stubs
from polugins_type_gen.cli import (
MissingVersionException,
NoNamespaceRegisteredException,
create_stubs,
)

typings_dir = Path("typings")

Expand All @@ -19,10 +23,29 @@ def typings_directory():


def test_cli(typings_directory):
create_stubs("0.19.13")
create_stubs("0.20.3")
lazyframe_stubs_path = (typings_dir / ExtensionClass.LAZYFRAME.import_path).with_suffix(".pyi")
lazyframe_stubs = lazyframe_stubs_path.read_text()
assert "external: example_package.PackageNamespace" in lazyframe_stubs
assert "pyproject: polugins._testing.namespaces.PyProjectNameSpace" in lazyframe_stubs
assert "config: polugins._testing.namespaces.ConfigNameSpace" in lazyframe_stubs
assert "env: polugins._testing.namespaces.EnvNameSpace" in lazyframe_stubs


def test_exception_on_no_namespaces(mocker):
mocker.patch(
"polugins_type_gen.cli._get_namespaces",
return_value={
ExtensionClass.EXPR: {},
ExtensionClass.SERIES: {},
ExtensionClass.LAZYFRAME: {},
ExtensionClass.DATAFRAME: {},
},
)
with pytest.raises(NoNamespaceRegisteredException):
create_stubs("0.20.3")


def test_exception_on_non_existing_version():
with pytest.raises(MissingVersionException):
create_stubs("9999.99.9")

0 comments on commit d5f661b

Please sign in to comment.