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

Retain literal types in collection backend #165

Merged
merged 11 commits into from
Feb 13, 2024

Conversation

jesper-friis
Copy link
Contributor

@jesper-friis jesper-friis commented Jan 3, 2024

Description:

Closes #160

Also added fallback_backend option to Triplestore.parse() and Triplestore.serialize() to allow calling parse() and serialize() with the collection backend (using rdflib under the hood).

Note that this PR builds on top of PR #161

Note also that this PR utilises DLite PR SINTEF/dlite#755. It is probably a good idea to merge that PR and create a new release of DLite before merging this PR to master.

Type of change:

  • Bug fix and code cleanup.
  • New feature.
  • Documentation update.
  • Testing.

Checklist for the reviewer:

This checklist should be used as a help for the reviewer.

  • Is the change limited to one issue?
  • Does this PR close the issue?
  • Is the code easy to read and understand?
  • Do all new feature have an accompanying new test?
  • Has the documentation been updated as necessary?

@jesper-friis jesper-friis linked an issue Jan 3, 2024 that may be closed by this pull request
@jesper-friis jesper-friis changed the base branch from master to 161-literal-equality January 3, 2024 18:02
@jesper-friis jesper-friis changed the base branch from 161-literal-equality to master January 25, 2024 09:34
@codecov-commenter
Copy link

codecov-commenter commented Jan 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c8e2fbd) 75.94% compared to head (4f532a6) 76.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #165      +/-   ##
==========================================
+ Coverage   75.94%   76.82%   +0.87%     
==========================================
  Files          17       17              
  Lines        1534     1536       +2     
==========================================
+ Hits         1165     1180      +15     
+ Misses        369      356      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +306 to +309
fallback_backend: If the current backend doesn't implement
parse, use the `fallback_backend` instead.
fallback_backend_kwargs: Dict with additional keyword arguments
for initialising `fallback_backend`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not see where these are tested. According to codecov, they are, so currently it should be OK. But it is important that this is tested on purpose and not "by accident", e.g. by using a backend that right now does not contain parse but might in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are tested in tests/backends/test_collection.py

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I prefer tests to be a bit more explicit.

Comment on lines +347 to +350
fallback_backend: If the current backend doesn't implement
serialisation, use the `fallback_backend` instead.
fallback_backend_kwargs: Dict with additional keyword arguments
for initialising `fallback_backend`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as for parse above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested in tests/backends/test_collection.py

Comment on lines 80 to 84
# Possible bug in mypy
# parse_object() is declared to return "Union[str, Literal]".
# Dispite of that complains mypy about that `v` has no
# attribute "value"
obj = v if isinstance(v, str) else str(v.value) # type: ignore
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
# Possible bug in mypy
# parse_object() is declared to return "Union[str, Literal]".
# Dispite of that complains mypy about that `v` has no
# attribute "value"
obj = v if isinstance(v, str) else str(v.value) # type: ignore
obj = v if isinstance(v, str) else str(v.value)

I have thested locally and with a draft pull request, I do not get any errors with mypy. Do you have an updated version?

Copy link
Contributor Author

@jesper-friis jesper-friis Feb 12, 2024

Choose a reason for hiding this comment

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

The problem came when I updated mypy. I got annoyed with it and changed the return type of utils.parse_object() from Union[str, Literal] to Union[str, Any] to get rid of these errors...

The source of the problem is that mypy suddenly has got the idea that Literal has got a special meaning, since it was added to typing.

It even doesn't understand if I change the return type of utils.parse_object() to Union[str, tripper.Literal].

Using Any in the return works around the problem. I have moved and updated the comment about buggy mypy to parse_object() and removed the # type: ignore as you suggested.

Comment on lines +297 to +298
fallback_backend="rdflib",
fallback_backend_kwargs=None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we didn't need to change backend/backend_kwargs to fallback_backend/fallback_backend_kwargs in the tests, it seems that we do not have adeguate tests.

Copy link
Contributor Author

@jesper-friis jesper-friis Feb 12, 2024

Choose a reason for hiding this comment

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

Well, the collection backend does not provide a parse() method, so it will use the fallback backend. That is tested when calling ts.parse() in tests/backends/test_collection.py.

Copy link
Contributor

@francescalb francescalb left a comment

Choose a reason for hiding this comment

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

Seems OK, although I do not understand the mypy issue.

@jesper-friis jesper-friis merged commit 138c2fa into master Feb 13, 2024
16 checks passed
@jesper-friis jesper-friis deleted the 160-retain-literal-types-in-collection-backend branch February 13, 2024 13:37
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.

Retain literal types in collection backend
3 participants