-
Notifications
You must be signed in to change notification settings - Fork 25
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] Added test of type checking and autoargs #71
base: master
Are you sure you want to change the base?
Conversation
@@ -1,2 +1,4 @@ | |||
numpy~=1.15 | |||
scipy~=1.5 | |||
autoclass |
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.
Checked: pure python (incl. dependencies) & easy to install it seems
@@ -1,2 +1,4 @@ | |||
numpy~=1.15 | |||
scipy~=1.5 | |||
autoclass | |||
typeguard |
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.
Checked: pure python (incl. dependencies) & easy to install it seems
PICMI_Python/applied_fields.py
Outdated
Bx_expression : Expression = None, | ||
By_expression : Expression = None, | ||
Bz_expression : Expression = None, | ||
lower_bound : VectorFloat3 = [None,None,None], |
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.
Is assigning a list as default argument value (old and new) actually possible?
Common Python bug:
https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments
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.
Yes, this code works as is, and I am aware of the issue. In this case, however, it is not expected that the argument, lower_bound
, will be modified, but only read. An alternative here would be to have lower_bound
default to None
. This would be more consistent with other vectors that default to None
. In this PR, I wanted to avoid changing the interface, but the change can be made.
I like the short form very much, this also gets very close to the python dataclass notation which performs almost the same by specifying class attributes. The following details raised my attention:
These are design principles, I've sent you an email for a short meeting on the matter to put these design principles into writing. |
Thanks for looking at this PR. Here are comments on the issues:
|
That sounds great to me. I would probably still introduce a |
This Once the changes here are accepted, I would suggest that this PR be merged. The other classes can be updated to use the new features in separate PRs (to keep the PRs small). This also allows the work to be spread out (since people are busy). |
PICMI_Python/base.py
Outdated
|
||
VectorFloat3 = typing.NewType('VectorFloat3', typing.Union[Sequence[float], np.ndarray[3, np.float64]]) | ||
VectorInt3 = typing.NewType('VectorInt3', typing.Union[Sequence[int], np.ndarray[3, np.int64]]) | ||
Expression = typing.NewType('Expression', str) |
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.
How do you fell about moving this into a separate namespace (for python a module i.e. here a file)?
This way every type will get a prefix and the distinction between python native & PICMI custom (and perhaps other types) will become clear.
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.
Yes, I was considering that, so that all of the type declarations are in the same place. It is somewhat tricky though because of the circular imports that will happen since some of the types depend on PICMI classes which will depend on the types. I'll see what I can put together.
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.
had that problem too, PEP 484 allows using strings instead of literals for forward declarations
But ultimately your call.
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.
Ok, thanks, using strings worked. I am clearly not the first person to come across this circular definition problem :)
PICMI_Python/diagnostics.py
Outdated
|
||
SpeciesType = typing.NewType('SpeciesType', typing.Union[particles.PICMI_Species, | ||
particles.PICMI_MultiSpecies]) | ||
SpeciesArgument = typing.NewType('SpeciesArgument', typing.Union[SpeciesType, Sequence[SpeciesType]]) |
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.
same as above: How do you feel about collecting all type definitions in a single file?
I am worried that existing type definitions might be overlooked and defined over and over again.
I've implemented the type checking in all of the classes. All of the defined types are not setup in the picmi_types.py files. Though, there is some kludginess there because of the way the type definitions that are in strings are evaluated. For example, for each type that is used, a module must import the modules used in the type definition (that's why particles.py must import fields for instance). The problem comes when a type is used that depends on the same module where the type is used. In this case, the module can't import itself, so the type string cannot reference the module. For this reason, I had to define two versions of the GridType, with and without the fields references An alternative is to do "*" imports, i.e. particles could do "from .fields import *" (so the fields prefix in the type definition is not needed), but this has its own messiness. I'm receptive to other alternative ideas. |
This is an alternative proposal to what PR #63 is doing. This set of changes is much smaller but still does all of the things desired.
codename_arg
with checking)__init__
routine or separate callable methodUnits tests need to be added, but there isn't much to test since it is using commonly used packages.
Documentation can be added (much of that from PR #63 can be carried over).