-
-
Notifications
You must be signed in to change notification settings - Fork 593
Treat RefResolver cache args as decorators. #333
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
Treat RefResolver cache args as decorators. #333
Conversation
This makes it more convenient to provide custom cache implementations.
This is kind of ugly. See also python-jsonschema/jsonschema#333
urljoin_cache_dec and remote_cache_dec can be used to provide caching decorators, while old urljoin_cache and remote_cache can still be used to directly provide callables.
I've made this change backwards compatible. New |
Hey -- sorry, I was sure I responded here with something about backwards compatibility but apparently I managed to not actually do that :/ Yeah! Backwards compatible is definitely necessary here -- I can see the pain, and will have a look at the changes, but yeah we'd need to preserve whatever's here regardless. Will try to post a more useful set of comments, I'm actually travelling so will have to find a bit of time to have a look, but really appreciated certainly. |
So as I'm getting ready for Draft 6, I'm noticing some more things wrong with the RefResolver interface which is making me think this needs a bit of thought before just doing. Specifically, There's also still just a ton of stupid methods on RefResolver -- having the *_scope methods be public really bothers me. I think this needs a bit of rethinking overall rather than patching things yet again, it's kind of a mess already :/ Thoughts / suggestions welcome of course! |
@Julian I actually wrote an id-processor / reference resolver in Python at one point, and then decided that when I eventually get to building a python hyper-schema client it would probably make more sense to use your validator as a base anyway. I can dig it up if you want- may not be suitable for direct use but it might be nice to compare implementation hazards. |
@handrews that'd definitely be appreciated! My inclination is actually to move ref resolution into a new external package, with a correct interface, and to deprecate everything here, after reimplementing it on top of it. |
I'll look into it on Monday. It's not too far off from being able to be its own library, although whether it's the API you'd want I don't know (I don't actually remember the exact state it's in- I last touched it around December, I think). |
@Julian I find current While on topic of reworking the |
Makes sense, will try to keep this use case in mind as well, we can probably come up with something that suits both that's still cleaner than what we've got here.
Would like to hear more about this. In the current way of dealing with things, this is the difference between |
I've made myself a Raising an exception from with resolver.resolving(ref) as resolved:
... It's AFAIK impossible to cleanly catch and handle an exception that originates from |
contextlib.ExitStack is the way to do that pretty much. But yeah context managers are a bit painful in these ways. The duplication there is part of the interface bloat I was referring to. |
I updated this pull request for the current master branch. As far as I can see, the remaining Sphinx "broken link" errors that are still thrown by Travis don't have anything to do with my changes. |
@avian2 hrm.. will have a look to see why Travis is complaining. Thanks for updating this, I still don't quite know how I feel about it :/, but it's been sitting here long enough that I'm starting to lean towards merging it despite my desire to revamp the whole interface. |
Dependency on mock has been removed from jsonschema (python-jsonschema#335).
Codecov Report
@@ Coverage Diff @@
## master python-jsonschema/jsonschema#333 +/- ##
==========================================
+ Coverage 96.31% 96.33% +0.02%
==========================================
Files 19 19
Lines 2386 2403 +17
Branches 312 314 +2
==========================================
+ Hits 2298 2315 +17
Misses 75 75
Partials 13 13 |
d17e1ba2 [242] Add pattern tests for numbers, objects, arrays, and null a5302a46 [329] Add tests for uniqueItems=false 0bf6358f [166] Add null-in-enum test 807591f0 Merge pull request #340 from karenetheridge/ether/markdown-conflict-markers e4c82f21 disambiguate underline from git conflict marker c4d3e0f9 Merge pull request #333 from zhxiaogg/tests-for-duration-format 618b30dc not all duration impl support year and month 926d0b3d tests for duration format 22eaffcd Merge pull request #331 from jdanyow/patch-2 b2e553e9 Merge pull request #330 from jdanyow/patch-1 9db34a17 remove unused constant cbf4654d fix typo in ref test description 44517887 Merge pull request #326 from awwright/master 0da2aaa1 Backport nested allOf/anyOf/oneOf tests 34a2dfa3 Add nested allOf, oneOf tests git-subtree-dir: json git-subtree-split: d17e1ba218bbd023d45182e014c0ce341bb36b16
Was this PR closed by mistake? I understand if you don't want to merge this. I'm just asking because the message in the squashed commit that's linked above makes it look like it includes my changes, but it doesn't actually. The GitHub issue # numbers don't match the repo names. |
Oy. Hi @avian2 It was indeed closed by mistake though not from the push (which yeah mingles issue #s across repos but it's a known issue), probably just me somehow misclicking while I wasn't paying attention. Going to reopen but yeah... somehow in all this time I still don't feel comfortable merging this, so to be honest I may re-close (hopefully intentionally) and just say we need to hit python-jsonschema/referencing#3. |
Currently, RefResolver class expects
urljoin_cache
andremote_cache
arguments to be functions that in turn callurljoin
andRefResolver.resolve_from_url
methods.This makes it inconvenient to provide a custom cache implementation: in order to pass it to the constructor, you need to make a function that calls the
resolve_from_url
method of a RefResolver instance before the said instance exists. This is possible, but usually ugly.With this change, the constructor treats these arguments as decorator objects. For instance, this makes
functools.lru_cache(16)
an acceptable value forremote_cache
. Existing docstrings already seem to suggest that this should be possible.I'm not sure how much the
urljoin_cache
andremote_cache
arguments are used in the wild. This change obviously breaks compatibility with existing code that uses them.