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

[WIP] Make ManifestSchema compatible with marshmallow >= 3 #3296

Closed
Closed
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
9 changes: 3 additions & 6 deletions platformio/commands/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from platformio.compat import dump_json_to_unicode
from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib
from platformio.package.manifest.parser import ManifestParserFactory
from platformio.package.manifest.schema import ManifestSchema, ManifestValidationError
from platformio.package.manifest.schema import ManifestSchema
from platformio.proc import is_ci
from platformio.project.config import ProjectConfig
from platformio.project.helpers import get_project_dir, is_platformio_project
Expand Down Expand Up @@ -495,11 +495,8 @@ def lib_register(config_url):
raise exception.InvalidLibConfURL(config_url)

# Validate manifest
data, error = ManifestSchema(strict=False).load(
ManifestParserFactory.new_from_url(config_url).as_dict()
)
if error:
raise ManifestValidationError(error, data)
manifest = ManifestParserFactory.new_from_url(config_url).as_dict()
ManifestSchema().load_manifest(manifest)

result = util.get_api_result("/lib/register", data=dict(config_url=config_url))
if "message" in result and result["message"]:
Expand Down
21 changes: 16 additions & 5 deletions platformio/package/manifest/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import requests
import semantic_version
from marshmallow import Schema, ValidationError, fields, validate, validates
from marshmallow import __version_info__ as marshmallow_version

from platformio.package.exception import ManifestValidationError
from platformio.util import memoized


class StrictSchema(Schema):
def handle_error(self, error, data):
def handle_error(self, error, data, *, _many, **_kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Python 2.7 doesn't have keyword only arguments

https://www.python.org/dev/peps/pep-3102/

Suggested change
def handle_error(self, error, data, *, _many, **_kwargs):
def handle_error(self, error, data, _many, **_kwargs):

# skip broken records
if self.many:
error.data = [
Expand All @@ -33,8 +34,10 @@ def handle_error(self, error, data):


class StrictListField(fields.List):
def _deserialize(self, value, attr, data):
def _deserialize(self, value, attr, data, **kwargs):
try:
if marshmallow_version >= (3, ):
return super(StrictListField, self)._deserialize(value, attr, data, **kwargs)
return super(StrictListField, self)._deserialize(value, attr, data)
except ValidationError as exc:
if exc.data:
Expand Down Expand Up @@ -142,9 +145,17 @@ class ManifestSchema(Schema):
)
)

def handle_error(self, error, data):
if self.strict:
raise ManifestValidationError(error, data)
# Wrapper to encapsulate differences between marshmallow 2 and 3
# load() method
def load_manifest(self, manifest):
if marshmallow_version >= (3, ):
data = self.load(manifest)
else:
data, _ = self.load(manifest)
return data

def handle_error(self, error, data, *, _many, **_kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def handle_error(self, error, data, *, _many, **_kwargs):
def handle_error(self, error, data, _many, **_kwargs):

No keyword only arguments in Python 2.7

raise ManifestValidationError(error, data)

@validates("version")
def validate_version(self, value): # pylint: disable=no-self-use
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"semantic_version>=2.8.1,<3",
"tabulate>=0.8.3,<1",
"pyelftools>=0.25,<1",
"marshmallow>=2.20.5,<3"
"marshmallow>=2.20.5"
]

setup(
Expand Down