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

Library Usage #118

Merged
merged 21 commits into from
Aug 24, 2023
Merged

Library Usage #118

merged 21 commits into from
Aug 24, 2023

Conversation

kchason
Copy link
Member

@kchason kchason commented Jul 25, 2023

Example usage of the programmatic usage of the CASE utilities.

import os

from case_utils.case_validate import validate, ValidationResult

# Validate a case file
result: ValidationResult = validate(os.path.abspath("location.json"), 'case-1.2.0')

print("Valid" if result.conforms else "Invalid")

@kchason kchason requested a review from a team as a code owner July 25, 2023 00:37
Copy link
Member

@ajnelson-nist ajnelson-nist left a comment

Choose a reason for hiding this comment

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

There are at least three can't-pass-as-is issues. It looks like a few parts of this PR can be filed on their own while we work those out.

case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
ajnelson-nist added a commit that referenced this pull request Jul 25, 2023
This patch partially implements a small piece of PR 118.

References:
* #118

Requested-by: kchason <[email protected]>
Signed-off-by: Alex Nelson <[email protected]>
ajnelson-nist pushed a commit that referenced this pull request Aug 9, 2023
AJN: This is a partial application of @kchason 's work in PR 118, and is
being pulled into its own patch series to focus review.

References:
* #118

Signed-off-by: Alex Nelson <[email protected]>
@ajnelson-nist ajnelson-nist added this to the 0.12.0 milestone Aug 14, 2023
No effects were observed on Make-managed files.

Signed-off-by: Alex Nelson <[email protected]>
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
case_utils/case_validate/__init__.py Outdated Show resolved Hide resolved
It seems to me like `--disallow-untyped-defs`, enabled in
`mypy --strict`, should have flagged this as an error.  However, from
documentation on `no-untyped-def`, `mypy` only requires
`__init__(...) -> None` when there is any argument aside from the first
`self`.  This patch follows the parenthetical recommendation from PEP
484 that `-> None` be given anyways.

References:
* https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-disallow-untyped-defs
* https://mypy.readthedocs.io/en/stable/error_code_list2.html#check-that-every-function-has-an-annotation-no-untyped-def
* https://peps.python.org/pep-0484/#the-meaning-of-annotations

Signed-off-by: Alex Nelson <[email protected]>
validate_result = pyshacl.validate(
validate_result: Tuple[
bool, Union[Exception, bytes, str, rdflib.Graph], str
] = pyshacl.validate(
Copy link
Member

Choose a reason for hiding this comment

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

I think this call needs to be replaced with the validate() method this PR is adding to this file, but only after logistics related to #123 are settled.

Copy link
Member

Choose a reason for hiding this comment

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

I believe this is addressed in 15f00c9.

_logger = logging.getLogger(os.path.basename(__file__))


class NonExistentCDOConceptWarning(UserWarning):
def validate(
input_file: str,
Copy link
Member

Choose a reason for hiding this comment

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

For our awareness, this is a narrower argument than the first argument of pyshacl.validate; here's today's definition:

https://github.com/RDFLib/pySHACL/blob/v0.23.0/pyshacl/validate.py#L369-L370

pyshacl.validate's first argument seems to permit a string to be a file path or URL, OR a full string dump of a graph. See these lines for heuristics in pyshacl.rdfutil.load.load_from_graph:

https://github.com/RDFLib/pySHACL/blob/v0.23.0/pyshacl/rdfutil/load.py#L222-L227

Should we implement "str means path" now, or just adopt the load_from_graph usage now from these lines:

https://github.com/RDFLib/pySHACL/blob/v0.23.0/pyshacl/validate.py#L424-L428

Copy link
Member

Choose a reason for hiding this comment

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

Welp, I found an issue pushing us towards expanding input_file from str. case_validate is written to take multiple input files as data graphs (as well as multiple input files as ontology graphs). I believe this behavior should be preserved, because otherwise a user that needs to read two data graphs at once needs to do some intermediary graph compilation before calling case_validate.

So, I think the first argument needs to become at least either Union[str, List[str]] or Union[str, Graph]. The current code path from the CLI entry point I think favors Union[str, List[str]].

Copy link
Member

Choose a reason for hiding this comment

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

I believe this is addressed in 15f00c9.

…idate

This patch separates implementation points between functionality
distinct to `case_utils.validate` and `pyshacl.validate`.  The
`allow_warnings` and `inference` parameters provide CASE-specific
documentation as an augmentation to `pyshacl.validate`'s documentation,
but otherwise other documentation on `pyshacl.validate`'s keyword
arguments is delegated to their upstream function.

This patch removes some hardcoded parameter values in
`pyshacl.validate`, letting the `case_validate` CLI or caller provide
any runtime-requested values.

Also, without functional impact, this patch sorts keyword parameters
alphabetically.

Signed-off-by: Alex Nelson <[email protected]>
Copy link
Member

@ajnelson-nist ajnelson-nist left a comment

Choose a reason for hiding this comment

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

I'm fine with this merging once CI passes. Also open to discussing any of the last revisions I made.

@kchason kchason merged commit bded343 into develop Aug 24, 2023
2 checks passed
@kchason kchason deleted the library-usage branch August 24, 2023 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants