Skip to content
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 hinting #192

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cf58e66
Initial type-hinting attempt
ggalloni Aug 27, 2024
bf001b5
Add try-except statements for ccl
ggalloni Aug 28, 2024
338cc84
Add type checking functions in `utils`
ggalloni Aug 29, 2024
2461901
Check types from yaml files
ggalloni Aug 29, 2024
5a12e70
Fix test
ggalloni Aug 29, 2024
ed2c841
Add tests with wrong types
ggalloni Aug 29, 2024
cec70c9
fix
ggalloni Sep 3, 2024
df199fb
compatibilty
ggalloni Sep 3, 2024
57de021
ops
ggalloni Sep 3, 2024
cbd7764
dynamic import
ggalloni Sep 3, 2024
62cf91d
Remove `CCL` and `Tracer` from hints
ggalloni Sep 3, 2024
0696e00
Avoid repetition of types and refactor
ggalloni Sep 3, 2024
d6863c0
Move type checking functions within `Cobaya`
ggalloni Sep 5, 2024
ff79ce9
Switch-on type enforcing
ggalloni Sep 5, 2024
4700060
Fix for `_fast_chi_square`
ggalloni Sep 5, 2024
501d279
Clean code
ggalloni Sep 10, 2024
3564490
Merge branch 'master' into type_hinting
ggalloni Sep 10, 2024
2ddec66
Remove some over-redundant hints
ggalloni Sep 10, 2024
a36a5dd
test conda install for `pyccl`, see [issue](https://github.com/LSSTDE…
ggalloni Sep 24, 2024
89fca30
test OS dependency
ggalloni Sep 24, 2024
3d4ec8c
ops
ggalloni Sep 24, 2024
aa76f23
test
ggalloni Sep 24, 2024
0e7a449
revert
ggalloni Sep 24, 2024
97679b3
Merge remote-tracking branch 'upstream/master' into type_hinting
ggalloni Oct 2, 2024
028955e
Fix `cobaya` version in requirements
ggalloni Oct 2, 2024
133e059
oopsie
ggalloni Oct 2, 2024
caba78b
Test new refactor of Cobaya type checking
ggalloni Nov 6, 2024
5fdfe05
Correct attribute name
ggalloni Nov 6, 2024
ba59550
Point to `master` branch of `cobaya`
ggalloni Nov 11, 2024
57c0169
add `typing_extensions` for compatibility
ggalloni Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add type checking functions in utils
  • Loading branch information
ggalloni committed Aug 29, 2024
commit 338cc84ccbfa1c3515f0ebbf09bda9d861d0e5b5
49 changes: 48 additions & 1 deletion soliket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""

from typing import Optional, Tuple, Dict
from typing import Optional, Tuple, Dict, Union, get_args, get_origin
from importlib import import_module

import numpy as np
Expand Down Expand Up @@ -73,3 +73,50 @@ def get_requirements(self) -> Dict[str, Dict[str, int]]:
"te": self.lmax,
"ee": self.lmax,
"bb": self.lmax, }}


def check_collection_type(value, expected_type, name):
origin_type = get_origin(expected_type)
if origin_type is list:
if isinstance(value, list):
item_type = get_args(expected_type)[0]
for item in value:
check_type(item, item_type, f"{name} item")
return True
elif origin_type is dict:
if isinstance(value, dict):
key_type, val_type = get_args(expected_type)
for key, val in value.items():
check_type(key, key_type, f"{name} key")
check_type(val, val_type, f"{name} value")
return True
return False


def check_type(value, expected_types, name: str) -> None:
if not isinstance(expected_types, tuple):
expected_types = (expected_types,)

if value is None:
return

for expected_type in expected_types:
origin_type = get_origin(expected_type)
if origin_type is None:
if isinstance(value, expected_type):
return
elif check_collection_type(value, expected_type, name):
return

msg = f"{name} must be of type {expected_types}, not {type(value)}"
raise TypeError(msg)


def check_yaml_types(
instance, attributes: Dict[str, Union[type, Tuple[type, ...]]]
) -> None:
for attr_name, expected_types in attributes.items():
value = getattr(instance, attr_name, None)
if value is None and isinstance(instance, dict):
value = instance.get(attr_name, None)
check_type(value, expected_types, attr_name)