-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP] Specify by Attributes #63
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
Open
s9105947
wants to merge
19
commits into
picmi-standard:master
Choose a base branch
from
s9105947:rework-dataclass
base: master
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.
Open
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e5e72c5
add unit test skeleton
s9105947 e1ab33f
fix note on pythonpath in Test/README.md
s9105947 14451ba
add classwithinit tests
s9105947 6be78e7
re-implement class with init
s9105947 3ef954d
transform PICMI_ConstantAppliedField to use reworked _ClassWithInit
s9105947 f18ef90
mv attribute docstrings
s9105947 892599f
s/dummy/placeholder/g
s9105947 079b916
mv testing placeholderclass inside of test class
s9105947 0881c67
self-check interface: add tests
s9105947 1d1f2f3
self-check interface: implement in base class
s9105947 02331cb
check interface: test for type checking
s9105947 b317ce8
check interface: add comment
s9105947 a4969f4
check interface: expand tests for typechecks
s9105947 72407f0
check interface: rename methods to accomodate type checks
s9105947 6c0dc94
implement type checks
s9105947 8323784
s/placeholder/mock/g
s9105947 e5000b1
add test for dict cast
s9105947 8c17e65
Revert "add test for dict cast"
s9105947 d1b3950
add init design documentation
s9105947 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| # Testing | ||
|
Member
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. Thank you for adding unit tests. Definitely something this repo needs. |
||
| PICMI is currently not thoroughly tested on its own. | ||
| Feel free to contribute! | ||
|
|
||
| [python unittest docs](https://docs.python.org/3.8/library/unittest.html) | ||
|
|
||
| ## Path Workaround | ||
| To allow `import picmistandard` to refer to the actual source of this repository, | ||
| this directory contains a symbolic link named `picmistandard` to the actual source. | ||
|
|
||
| By supplying an appropriate `PYTHONPATH` this module is loaded. | ||
| (The current directory is always available for imports, so this is not necessarily required.) | ||
|
|
||
| ## Unittests | ||
| Unittests are launched from the `__main__` function from the unittest directory. | ||
| This tests the currently available module `picmistandard`. | ||
|
|
||
| The file structure follows the source 1-to-1. | ||
|
|
||
| To test the development version run (from this directory): | ||
|
|
||
| ``` | ||
| python -m unit | ||
| ``` | ||
|
|
||
| ## E2E | ||
| Execute the example as end-to-end test by launching `./launch_e2e_test.sh` from this directory. | ||
| Note that it requires the python module `fbpic` to be available. | ||
File renamed without changes.
This file contains hidden or 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 @@ | ||
| ../PICMI_Python |
This file contains hidden or 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 @@ | ||
| from .base import * |
This file contains hidden or 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,4 @@ | ||
| from . import * | ||
|
|
||
| import unittest | ||
| unittest.main() |
This file contains hidden or 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,100 @@ | ||
| import picmistandard | ||
| import unittest | ||
| import typing | ||
|
|
||
|
|
||
| class DummyClass(picmistandard.base._ClassWithInit): | ||
s9105947 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # note: refer to .base b/c class name with _ will not be exposed | ||
| mandatory_attr: typing.Any | ||
| name = "" | ||
| optional = None | ||
| _protected = 1 | ||
|
|
||
|
|
||
| class Test_ClassWithInit(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| picmistandard.register_codename("dummypic") | ||
|
|
||
| def test_arguments_used(self): | ||
| """init sets provided args to attrs""" | ||
| d = DummyClass(mandatory_attr=None, | ||
| name="n", | ||
| optional=17) | ||
| self.assertEqual(None, d.mandatory_attr) | ||
| self.assertEqual("n", d.name) | ||
| self.assertEqual(17, d.optional) | ||
|
|
||
| def test_defaults(self): | ||
| """if not given, defaults are used""" | ||
| d = DummyClass(mandatory_attr=42) | ||
| self.assertEqual("", d.name) | ||
| self.assertEqual(None, d.optional) | ||
|
|
||
| def test_unkown_rejected(self): | ||
| """unknown names are rejected""" | ||
| with self.assertRaisesRegex(NameError, ".*blabla.*"): | ||
| DummyClass(mandatory_attr=1, | ||
| blabla="foo") | ||
|
|
||
| def test_codespecific(self): | ||
| """arbitrary attrs for code-specific args used""" | ||
| # args beginning with dummypic_ must be accepted | ||
| d1 = DummyClass(mandatory_attr=2, | ||
| dummypic_foo="bar", | ||
| dummypic_baz="xyzzy", | ||
| dummypic=1, | ||
| dummypic_=3) | ||
| self.assertEqual("bar", d1.dummypic_foo) | ||
| self.assertEqual("xyzzy", d1.dummypic_baz) | ||
| self.assertEqual(1, d1.dummypic) | ||
| self.assertEqual(3, d1.dummypic_) | ||
|
|
||
| # _ separator is required: | ||
| with self.assertRaisesRegex(NameError, ".*dummypicno_.*"): | ||
| DummyClass(mandatory_attr=2, | ||
| dummypicno_="None") | ||
|
|
||
| # args from other supported codes are still accepted | ||
| d2 = DummyClass(mandatory_attr=None, | ||
| warpx_anyvar=1, | ||
| warpx=2, | ||
| warpx_=3, | ||
| fbpic=4) | ||
| self.assertEqual(None, d2.mandatory_attr) | ||
| self.assertEqual(1, d2.warpx_anyvar) | ||
| self.assertEqual(2, d2.warpx) | ||
| self.assertEqual(3, d2.warpx_) | ||
| self.assertEqual(4, d2.fbpic) | ||
|
|
||
| def test_mandatory_enforced(self): | ||
| """mandatory args must be given""" | ||
| with self.assertRaisesRegex(RuntimeError, ".*mandatory_attr.*"): | ||
| DummyClass() | ||
|
|
||
| # ok: | ||
| d = DummyClass(mandatory_attr="x") | ||
| self.assertEqual("x", d.mandatory_attr) | ||
|
|
||
| def test_no_typechecks(self): | ||
| """no typechecks, explicit type annotations are rejected""" | ||
| class WithTypecheck(picmistandard.base._ClassWithInit): | ||
| attr: str | ||
| num: int = 0 | ||
|
|
||
| with self.assertRaises(SyntaxError): | ||
| # must complain purely b/c typecheck is *there* | ||
| # (even if it would enforceable) | ||
| WithTypecheck(attr="d", num=2) | ||
|
|
||
| def test_protected(self): | ||
| """protected args may *never* be accessed""" | ||
| with self.assertRaisesRegex(NameError, ".*_protected.*"): | ||
| DummyClass(mandatory_attr=1, | ||
| _protected=42) | ||
|
|
||
| # though, *technically speaking*, it can be assigned | ||
| d = DummyClass(mandatory_attr=1) | ||
| # ... this is evil, never do this! | ||
| d._protected = 3 | ||
| self.assertEqual(3, d._protected) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
note: hard to enforce, maybe even error-prune API contract and maybe not ideal for something as common as
__init__