Skip to content

Commit

Permalink
fix: handle tomlkit.items.Float and Integer
Browse files Browse the repository at this point in the history
Pipenv's integration tests reveal that plette's test suite
was a bit too simplistic.

tomlkit parsed the following package spec:

zipp = 1.11

as a tomlkit.items.Float, which causes the validation to break.
This commit handles this and add unit and integration test case
for this.

Signed-off-by: Oz Tiram <oz.tiram@gmail.com>
  • Loading branch information
oz123 committed Apr 20, 2024
1 parent e03c20b commit e3fa787
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions examples/Pipfile.ok.extras-list
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# package extras are a list
[packages]
msal = {version= "==1.20.0", extras = ["broker"]}
six = 1.11
zipp = "*"

9 changes: 4 additions & 5 deletions src/plette/models/packages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import tomlkit

from .base import DataModel, DataValidationError

class PackageSpecfiers(DataModel):
Expand Down Expand Up @@ -27,18 +29,15 @@ class Package(DataModel):

@classmethod
def validate(cls, data):
if not isinstance(data, (str, dict)):
raise DataValidationError(f"invalid type for package: {type(data)}")

if isinstance(data, str):
if isinstance(data, (str, tomlkit.items.Float, tomlkit.items.Integer)):
return
if isinstance(data, dict):
PackageSpecfiers.validate(data)
else:
raise DataValidationError(f"invalid type for package data: {type(data)}")

def __getattr__(self, key):
if isinstance(self._data, str):
if isinstance(self._data, (str, tomlkit.items.Float, tomlkit.items.Integer)):
if key == "version":
return self._data
raise AttributeError(key)
Expand Down
14 changes: 12 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib

import tomlkit.items
import pytest

from plette import models
Expand Down Expand Up @@ -107,14 +108,23 @@ def test_package_wrong_key():
p.version
assert str(ctx.value) == "version"


def test_package_with_wrong_specfiers():
with pytest.raises(models.base.DataValidationError) as ctx:
_ = models.Package(1.2)
assert str(ctx.value) == "invalid type for package: <class 'float'>"
assert str(ctx.value) == "invalid type for package data: <class 'float'>"


def test_package_with_specfiers():
value = 1.2
float_value = tomlkit.items.Float(value, tomlkit.items.Trivia(), str(value))
p = models.Package(float_value)
assert p.version == float_value


def test_package_with_wrong_extras():
with pytest.raises(models.base.DataValidationError):
p = models.Package({"version": "==1.20.0", "extras": "broker"})
_ = models.Package({"version": "==1.20.0", "extras": "broker"})


def test_package_with_extras():
Expand Down

0 comments on commit e3fa787

Please sign in to comment.