Skip to content

Conversation

@fjosw
Copy link
Owner

@fjosw fjosw commented Dec 25, 2024

As a first step towards adding type hints, I added automatic type hints with monkeytype. Hints for the more complicated methods will need additional manual work but for the simpler methods this seems to have worked quite well.

@fjosw fjosw marked this pull request as ready for review January 3, 2025 18:12
@fjosw
Copy link
Owner Author

fjosw commented Jan 3, 2025

Not quite done but already in decent shape for a review @s-kuberski @jkuhl-uni:

  • Most of the changes are just type annotations
  • I changed a few lines to help mypy with understanding the type annotations
  • I fixed a few smaller bugs in which explicit type checks were not consistent or did not cover certain edge cases

For now I ignored the input/* modules and most of correlators.py, the remaining modules only raise a few smaller mypy errors which I wasn't able to fix right away.

@s-kuberski
Copy link
Collaborator

Great, thanks a lot for all of your work! I'll have a look in the upcoming week.

__slots__ = ["content", "N", "T", "tag", "prange"]

def __init__(self, data_input, padding=[0, 0], prange=None):
def __init__(self, data_input: Any, padding: list[int]=[0, 0], prange: Optional[list[int]]=None):
Copy link
Collaborator

@jkuhl-uni jkuhl-uni Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be list[Obs]? Alternatively, in the list, there could be something Obs-like, I suppose, such as list[CObs]?

@jkuhl-uni
Copy link
Collaborator

Hey @fjosw,
thanks for all the work, I am looking through it right now, it looks excellent to me. I hope it was okay that I pushed a few small changes. If not, feel free to revert them, as you please :)

@fjosw fjosw requested a review from jkuhl-uni May 7, 2025 15:28
@jkuhl-uni
Copy link
Collaborator

Hi,
I looked through the code again and made mainly small changes, as to improve some type hints.
The PR looks good to me otherwise.

N_sigma_dict: dict[str, int] = {}

def __init__(self, samples, names, idl=None, **kwargs):
def __init__(self, samples: list[Union[ndarray, list[Any]]], names: list[str], idl: Optional[Union[list[list[int]], list[Union[list[int], range]], list[range]]]=None, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could one make this more concrete? Is this Union[None, float, int]?
Or if this used as we also allow np types?



def read_ExternalLeg_hd5(path, filestem, ens_id, idl=None):
def read_ExternalLeg_hd5(path: str, filestem: str, ens_id: str, idl: Optional[range]=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def read_ExternalLeg_hd5(path: str, filestem: str, ens_id: str, idl: Optional[range]=None):
def read_ExternalLeg_hd5(path: str, filestem: str, ens_id: str, idl: Optional[Union[list, range]]=None):



def extract_t0_hd5(path, filestem, ens_id, obs='Clover energy density', fit_range=5, idl=None, **kwargs):
def extract_t0_hd5(path: str, filestem: str, ens_id: str, obs='Clover energy density', fit_range: int=5, idl: Optional[range]=None, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def extract_t0_hd5(path: str, filestem: str, ens_id: str, obs='Clover energy density', fit_range: int=5, idl: Optional[range]=None, **kwargs):
def extract_t0_hd5(path: str, filestem: str, ens_id: str, obs='Clover energy density', fit_range: int=5, idl: Optional[Union[list, range]]=None, **kwargs):



def read_meson_hd5(path, filestem, ens_id, meson='meson_0', idl=None, gammas=None):
def read_meson_hd5(path: str, filestem: str, ens_id: str, meson: str='meson_0', idl: Optional[range]=None, gammas: Optional[tuple[str, ...]]=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def read_meson_hd5(path: str, filestem: str, ens_id: str, meson: str='meson_0', idl: Optional[range]=None, gammas: Optional[tuple[str, ...]]=None):
def read_meson_hd5(path: str, filestem: str, ens_id: str, meson: str='meson_0', idl: Optional[Union[list, range]]=None, gammas: Optional[tuple[str, ...]]=None):



def read_hd5(filestem, ens_id, group, attrs=None, idl=None, part="real"):
def read_hd5(filestem: str, ens_id: str, group: str, attrs: Optional[Union[dict, int]]=None, idl=None, part="real") -> Corr:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def read_hd5(filestem: str, ens_id: str, group: str, attrs: Optional[Union[dict, int]]=None, idl=None, part="real") -> Corr:
def read_hd5(filestem: str, ens_id: str, group: str, attrs: Optional[Union[dict, int]]=None, idl:Optional[Union[list, range]]=None, part="real") -> Corr:



def read_Fourquark_hd5(path, filestem, ens_id, idl=None, vertices=["VA", "AV"]):
def read_Fourquark_hd5(path: str, filestem: str, ens_id: str, idl: Optional[range]=None, vertices: list[str]=["VA", "AV"]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def read_Fourquark_hd5(path: str, filestem: str, ens_id: str, idl: Optional[range]=None, vertices: list[str]=["VA", "AV"]):
def read_Fourquark_hd5(path: str, filestem: str, ens_id: str, idl: Optional[Union[list, range]]=None, vertices: list[str]=["VA", "AV"]):

Comment on lines +79 to 85
path = kwargs.get('path')
if path is not None:
if not isinstance(path, str):
raise Exception("Path has to be a string.")
file_name = path + '/' + name + '.p'
else:
file_name = name + '.p'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path = kwargs.get('path')
if path is not None:
if not isinstance(path, str):
raise Exception("Path has to be a string.")
file_name = path + '/' + name + '.p'
else:
file_name = name + '.p'
path = kwargs.get('path', '.')
if not isinstance(path, str):
raise Exception("Path has to be a string.")
file_name = path + '/' + name + '.p'

intern[name]["spec"][quarks][off][w][w2]["pattern"] = _make_pattern(version, name, off, w, w2, intern[name]['b2b'], quarks)
else:
intern[name]["spec"][quarks][off][w]["0"] = {}
intern[name]["spec"][quarks][off][w]["0"]["pattern"] = _make_pattern(version, name, off, w, 0, intern[name]['b2b'], quarks)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
intern[name]["spec"][quarks][off][w]["0"]["pattern"] = _make_pattern(version, name, off, w, 0, intern[name]['b2b'], quarks)
intern[name]["spec"][quarks][off][w]["0"]["pattern"] = _make_pattern(version, name, off, w, "0", intern[name]['b2b'], quarks)



def _make_pattern(version, name, noffset, wf, wf2, b2b, quarks):
def _make_pattern(version: str, name: str, noffset: str, wf: str, wf2: Union[str, int], b2b: bool, quarks: str) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _make_pattern(version: str, name: str, noffset: str, wf: str, wf2: Union[str, int], b2b: bool, quarks: str) -> str:
def _make_pattern(version: str, name: str, noffset: str, wf: str, wf2: str, b2b: bool, quarks: str) -> str:

Comment on lines +670 to 676
path = kwargs.get('path')
if path is not None:
if not isinstance(path, str):
raise TypeError('path has to be a string.')
file_name = path + '/' + filename
else:
file_name = filename
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path = kwargs.get('path')
if path is not None:
if not isinstance(path, str):
raise TypeError('path has to be a string.')
file_name = path + '/' + filename
else:
file_name = filename
path = kwargs.get('path', '.')
if not isinstance(path, str):
raise TypeError('path has to be a string.')
file_name = path + '/' + filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants