-
Notifications
You must be signed in to change notification settings - Fork 15
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
Scala sealed trait ability (mostly) in dataconf using dataclass #10
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d3a940b
added the ability to read nested dataclass configs replicating Scala …
dwsmith1983 e210aff
Merge pull request #1 from dwsmith1983/dev/sealed-trait-type-behavior
dwsmith1983 30e04ea
removed print statment
dwsmith1983 e42a951
updated README to demostrate sealed traits usage with dataconf and da…
dwsmith1983 e26cfca
created version.py for __version__ which will populate from the libra…
ef4b407
called InputType and added return to sealed_trait
02da544
added a second method to test for nested dataclass
691f3f6
cleaned up fixed todo for the sealed trait mock testing
e8d32c3
pre-commit doesnt __version__ is there a way to override this?
115b0b4
I see pre-commit looks at __all__; not really familiar with this library
2d0be26
fixed relative to abs import in test, removed old comment, removed li…
dwsmith1983 50ea7e9
updated the readme example to parallel the test
dwsmith1983 5ad6f0d
need to grab string name __name__ for getattr to use the new for loop
a3d9763
abstract input class with dataclass override
80fca49
added @zifeo suggestion
39552be
updated README
9537353
removed conf assignment
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -113,6 +113,26 @@ def __parse(value: any, clazz, path): | |
if clazz is ConfigTree: | ||
return __parse_type(value, clazz, path, isinstance(value, ConfigTree)) | ||
|
||
# Todo: this should be cleaner | ||
# iterates through class dict to check for subclasses | ||
# if subclasses are a dataclass parse values | ||
# the idea here is to replicate parsing of a sealed trait in Scala | ||
# when using pureconfig | ||
child_failures = [] | ||
for child_clazz in sorted(clazz.__subclasses__(), key=lambda c: c.__name__): | ||
if is_dataclass(child_clazz): | ||
try: | ||
return __parse(value, child_clazz, path) | ||
except TypeConfigException as f: | ||
child_failures.append(str(f)) | ||
|
||
# no need to check length; false if empty | ||
if child_failures: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I removed the length check @zifeo |
||
fails = "\n- ".join(child_failures) | ||
raise TypeConfigException( | ||
f"expected type {clazz} at {path}, failed subclasses:{fails}" | ||
) | ||
|
||
raise TypeConfigException(f"expected type {clazz} at {path}, got {type(value)}") | ||
|
||
|
||
|
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,3 @@ | ||
import importlib.metadata | ||
|
||
__version__ = importlib.metadata.version("dataconf") | ||
dwsmith1983 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,30 @@ | ||
from dataclasses import dataclass | ||
from typing import Text | ||
|
||
|
||
class InputType: | ||
pass | ||
|
||
|
||
@dataclass(init=True, repr=True) | ||
class StringImpl(InputType): | ||
name: Text | ||
age: Text | ||
|
||
def test_method(self): | ||
return f"{self.name} is {self.age} years old." | ||
|
||
def test_complex(self): | ||
return int(self.age) * 3 | ||
|
||
|
||
@dataclass(init=True, repr=True) | ||
class IntImpl(InputType): | ||
area_code: int | ||
phone_num: Text | ||
|
||
def test_method(self): | ||
return f"The area code for {self.phone_num} is {str(self.area_code)}" | ||
|
||
def test_complex(self): | ||
return self.area_code - 10 |
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
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.
@zifeo also changed
f
tostr(f)
for the join. We will need a test for failure too to make sure all is working.