-
Notifications
You must be signed in to change notification settings - Fork 519
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] new config system, 1.2 tagset support #700
Draft
nitzmahone
wants to merge
8
commits into
yaml:main
Choose a base branch
from
nitzmahone:yaml12_plus_config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,712
−495
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56644b3
XXX - temporarily disable most CI
nitzmahone 34e764f
Fix issue with representing Enum types
Thom1729 f8e89b3
Add a test for the YAML 1.1 types
perlpunk 35f0417
XXX - remove appveyor.yml file for now
ingydotnet 623a36e
Move around methods for better inheritance
perlpunk 31cd170
Add support for YAML 1.2 schemas
perlpunk b88bd44
Add experimental wrappers around YAML 1.2 tags code
perlpunk 5e566d8
WIP tagset config
nitzmahone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
from functools import partialmethod | ||
from .tagset import TagSet | ||
|
||
T = t.TypeVar('T') | ||
|
||
|
||
class LoaderConfigMixin: | ||
@classmethod | ||
# FIXME: fix tagset type to use DataClasses, at least externally? | ||
def config(cls: type[T], type_name: str | None = None, tagset: TagSet | ... = ..., **kwargs) -> type[T]: | ||
if not type_name: | ||
# FIXME: hash the inputs for a dynamic type name and cache it? | ||
type_name = f'abcd_from_{cls.__name__}' | ||
|
||
new_type = t.cast(cls, type(type_name, (cls, ), {})) | ||
|
||
# FIXME: add support for arbitrary kwargs passthru ala dumper? | ||
|
||
if tagset is not ...: | ||
# FIXME: provide a base class hook/method for this reset | ||
new_type.yaml_implicit_resolvers = {} | ||
new_type.init_resolvers(tagset.resolvers) | ||
new_type.yaml_constructors = {} | ||
new_type.init_constructors(tagset.constructors) | ||
|
||
return new_type | ||
|
||
|
||
class DumperConfigMixin: | ||
@classmethod | ||
def config(cls: type[T], type_name: str | None = None, | ||
tagset: TagSet | ... = ..., | ||
# FIXME: make some of the more obscure style things "nicer" (eg enums?) or just pass through existing values? | ||
default_style: str | ... = ..., default_flow_style: bool | ... = ..., | ||
# FIXME: properly type-annotate the rest of these | ||
canonical=..., indent=..., width=..., | ||
allow_unicode=..., line_break=..., | ||
encoding=..., explicit_start=..., explicit_end=..., | ||
version=..., tags=..., sort_keys=..., | ||
**kwargs) -> type[T]: | ||
|
||
if not type_name: | ||
# FIXME: hash the inputs for a dynamic type name and cache it? | ||
type_name = f'abcd_from_{cls.__name__}' | ||
|
||
# preserve wrapped config defaults for values where we didn't get a default | ||
# FIXME: share this code with the one in __init__.dump_all (and implement on others) | ||
dumper_init_kwargs = dict( | ||
default_style=default_style, | ||
default_flow_style=default_flow_style, | ||
canonical=canonical, indent=indent, width=width, | ||
allow_unicode=allow_unicode, line_break=line_break, | ||
encoding=encoding, version=version, tags=tags, | ||
explicit_start=explicit_start, explicit_end=explicit_end, sort_keys=sort_keys, **kwargs) | ||
|
||
dumper_init_kwargs = {k: v for k, v in dumper_init_kwargs.items() if v is not ...} | ||
|
||
patched_init = partialmethod(cls.__init__, | ||
**dumper_init_kwargs) | ||
|
||
new_type = t.cast(cls, type(type_name, (cls, ), {'__init__': patched_init})) | ||
|
||
# FIXME: support all the dynamic dispatch types (multi*, etc) | ||
if tagset is not ...: | ||
# FIXME: provide a base class hook/method for this reset | ||
new_type.yaml_implicit_resolvers = {} | ||
new_type.init_resolvers(tagset.resolvers) | ||
new_type.yaml_representers = {} | ||
new_type.init_representers(tagset.representers) | ||
|
||
return new_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would probably be better done with
inspect.signature()
; then it's 100% dynamic off whatever init args the current class accepts