Skip to content

Commit

Permalink
Add typehints (#406)
Browse files Browse the repository at this point in the history
* Minor refactoring

- Replace 1 - CDF by SF (survival function), which returns the
  same value, but can be more numerically stable.
- Raise ValueError for invalid values of t_input.

* Add explicit type conversion from np.ndarray to float

np.var() can also return a numpy array (when applied element-wise
to a numpy array). Adding the explicit conversion to float will
make sure the function "fails gracefully" if this is the case,
that is, if the output type does not match the expected type.

* Remove extra empty line for PEP8 compliance

* Minor refactoring

- Remove redundant parentheses around assert: assert is a Python
  keyword, not a "classical" function, hence no parentheses are needed.
- Add extra else-clause to make sure nimages is never referenced before
  assignment.
- Add typecast to make sure set_all() receives the expected input type.

* Convert class methods that to do not use self to staticmethod

* Minor refactoring

- Add typecasting for set_all() to match expected type
- Remove redundant parentheses for assert
- Improve PEP8 compliance

* Remove unused variables

* Remove unnecessary variable assignment

* Remove unused import

* Change value of `center` to match expected type

* Add type hints to pynpoint.core

* Add type hints to pynpoint.processing

* Add type hints to pynpoint.readwrite

* Add and update type hints for pynpoint.util

* Add type hints to tests.test_core

* Add type hints for tests.test_processing

* Add type hints for tests.test_readwrite

* Add two new custom type hints: StaticAttribute and NonStaticAttribute

These type hints are basically just syntactic sugar for the sets of
types of values that are acceptable as static and non-static attributes
in a PynPoint database.

For now, NonStaticAttribute also encompasses tuples and lists, because
the unit tests sometimes call functions with the "wrong" type. Once the
unit tests have been updated to use only numpy arrays for non-static
attributes, this can be updated accordingly.

* Add missing `@typechecked` decorators to pynpoint.core

Additionally, fix all type hints that were shown to be wrong or
insufficient by running the type checker.

* Fix attribute type: must be a numpy array, not a range() object

* Replace illegal type for static attribute

Static attributes should be int, float or str (or the numpy versions of
these types); not list.

* Remove lines from test that have no effect

* Remove unused import

* Add missing `@typechecked` decorators to functions in pynpoint.processing

Additionally:
- Fix all type hints that were shown to be wrong or insufficient by
  running the type checker.
- Add a few type conversions where needed to ensure that functions
  return the promised type (e.g., cast to Python integer).

* Add missing `@typechecked` decorators to functions in pynpoint.util

Additionally, fix all type hints that were shown to be wrong or
insufficient by running the type checker.

* Add type cast to ensure `pca_number` always receives a Python integer

* Add missing `@typechecked` decorators to pynpoint.core.pypeline; fix tests

Two unit tests had to be changed to allow typechecking using the
`@typechecked` decorator here:
- test_add_wrong_module(): With the type check in place, a TypeError
  is raised by the typeguard library before the assertion inside the
  add_module() method is reached. Therefore, the test was adjusted to
  check for this TypeError instead of the assertion, which now became
  pointless and was removed from the add_module() method.
- test_run_module_wrong_tag(): The expected output for calling the
  _validate() method with a str argument, i.e. the tuple (False, None),
  is impossible to reach because the type check causes a TypeError to
  be raised in that case. Therefore, the test was changed to check for
  this TypeError instead.

* Add comments for functions that cannot be decorated with `@typechecked`

* Add `@typechecked` decorator and adjust type hints

* Improve type hints and update documentation

* Update documentation

* Update documentation

* Add double colon back (necessary for docs to render correctly)

* Update documentation

Co-authored-by: Tomas Stolker <tomas.stolker@phys.ethz.ch>
  • Loading branch information
timothygebhard and Tomas Stolker committed Apr 3, 2020
1 parent 4f9cf5e commit 7576a83
Show file tree
Hide file tree
Showing 73 changed files with 1,108 additions and 770 deletions.
7 changes: 6 additions & 1 deletion pynpoint/core/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
Module to obtain information about the implemented attributes.
"""

from typing import Dict, Union

def get_attributes():
from typeguard import typechecked


@typechecked
def get_attributes() -> Dict[str, Dict[str, Union[str, float, int, None]]]:
"""
Function to get a dictionary with all attributes.
Expand Down

0 comments on commit 7576a83

Please sign in to comment.