-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add type hints and apply mypy fix for function attributes where needed #350
Conversation
This will require adding the |
You can add it to |
Codecov Report
@@ Coverage Diff @@
## master #350 +/- ##
==========================================
+ Coverage 72.47% 72.53% +0.06%
==========================================
Files 173 173
Lines 55631 55775 +144
==========================================
+ Hits 40316 40459 +143
- Misses 15315 15316 +1
|
The code coverage is slightly reduced, and the tests are therefore not completed, but this is due to the lines added after the |
Perhaps we can add something like the following to our
|
@LegrandNico can you rebase the branch to resolve conflicts? |
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.
Great work! I'm not too familiar with the Protocol
decorator workaround, but otherwise this PR looks good to me - I have some minor nitpicky comments, but since all tests pass I think this PR is good to go.
aesara/graph/callcache.py
Outdated
|
||
def persist(self, filename=None): | ||
def persist(self, filename: Optional[str] = None) -> NoReturn: |
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.
Shouldn't this be -> None
?
The NoReturn
return type is for functions that never return normally, not functions that just don't return a value [1] [2].
[1] https://mypy.readthedocs.io/en/stable/more_types.html#the-noreturn-type
[2] https://www.python.org/dev/peps/pep-0484/#the-noreturn-type
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.
I have changed this to -> None
.
aesara/graph/callcache.py
Outdated
@@ -46,7 +47,7 @@ def call(self, fn, args=(), key=None): | |||
_logger.debug("cache hit %i", len(self.cache)) | |||
return self.cache[key] | |||
|
|||
def __del__(self): | |||
def __del__(self) -> NoReturn: |
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.
Similarly here with NoReturn
.
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.
Idem
aesara/tensor/basic.py
Outdated
@@ -305,6 +313,8 @@ def get_scalar_constant_value( | |||
|
|||
Parameters | |||
---------- | |||
orig_v : |
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.
Missing docstring? We should at least add the type here.
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.
I have added this:
"""
orig_v : int, float, np.ndarray, Constant or None
The variable `v` where to find the constant scalar(0-D).
"""
_is_nonzero = True | ||
_is_nonzero: bool = True | ||
real: Optional[property] = None | ||
imag: Optional[property] = 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.
Not sure what this is - this looks like new code?
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.
If I remember correctly, I had to add imag
and real
so https://github.com/pymc-devs/aesara/blob/25162ed0f8949aa2509e0b1a04ddfe71b03c9fdb/aesara/tensor/math.py#L1410 does not return an error.
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 PR brings significant improvements of signatures in many places. I rebased it so we can get this going again 🚀
In one file I found type hints that I think are too restrictive.
@@ -45,15 +45,15 @@ def force_unlock(lock_dir: os.PathLike): | |||
|
|||
|
|||
@contextmanager | |||
def lock_ctx(lock_dir: os.PathLike = None, *, timeout: Optional[float] = None): | |||
def lock_ctx(lock_dir: Optional[os.PathLike] = None, *, timeout: Optional[float] = -1): |
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.
def lock_ctx(lock_dir: Optional[os.PathLike] = None, *, timeout: Optional[float] = -1): | |
def lock_ctx(lock_dir: Optional[os.PathLike] = None, *, timeout: Optional[float] = None): |
It was intentionally None
, because this way it defaults to the config setting.
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.
+1
on_unused_input=None, | ||
inputs: "List[Variable]", | ||
outputs: "Optional[List[Variable]]" = None, | ||
mode: Union[str] = 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.
mode: Union[str] = None, | |
mode: Union[str, "Mode"] = None, |
And further above in the TYPE_CHECKING
block from aesara.compile.mode import Mode
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.
Shouldn't the type-hint include Optional
?
@@ -1079,7 +1098,7 @@ def grad(self, inp, grads): | |||
_nonzero = Nonzero() | |||
|
|||
|
|||
def nonzero(a, return_matrix=False): | |||
def nonzero(a: np.array, return_matrix: bool = False) -> Tuple: |
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.
All these functions can take not only numpy arrays, but also TensorVariables
or in some cases lists.
The type hint from line 1042 is probably a good candidate for most functions in this file.
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.
+1
I accidentally closed this one by renaming the branch |
Looks like this branch itself was deleted because it was your |
This PR adds various type hints to increase type coverage. It also fixes the mypy errors raised by the use of function attributes, using the workaround described here.
It will contribute to #200.
Several of the remaining errors returned by mypy are related to inconsistent type inherited from core classes (e.g
Variable
) and I think this is the next thing to be fixed if we want to solve the rest. I saw what is proposed in #134, but I think I will need a bit more guidance for the next step.