sisl icon indicating copy to clipboard operation
sisl copied to clipboard

type hint as much code as possible

Open zerothi opened this issue 2 years ago • 2 comments

Can I create a PR to type hint as much code as possible? I'm adding typehints to all nodes to be able to infer an input field from them and it would be useful to have sisl type hinted, otherwise the checker gives me errors. It also would be useful to have some usual types defined in sisl to reuse them (e.g. what can go into an atoms argument). Two questions:

  • Does it make sense to have a sisl.typing module where we define types that are used throughout sisl?
  • Are you in favour of having overloads to specify the output type based on input type?

Originally posted by @pfebrer in https://github.com/zerothi/sisl/issues/446#issuecomment-1076265556

Yes, as you can see I started some of it, but it is a lot of work ;)

I think we should have the typing module as that seems like the standard way to do it.

Are there ways to construct the typing classes dynamically (ie the singledispatch method grabs the input type, but can we build the atoms type in the same go? (I think not but...)

As for the overloads. Do you mean like the singledispatch? Could you perhaps give an example? I would not be in favour of duplicating code, just for type hinting. And if a function has different return types depending on input types then we should probably refactor it. Here the read routines are good candidates... Probably we should think of ways of standardising return types... I. E. Other ways than having as_* arguments... Any ideas?

zerothi avatar Mar 27 '22 10:03 zerothi

Are there ways to construct the typing classes dynamically (ie the singledispatch method grabs the input type, but can we build the atoms type in the same go? (I think not but...)

I don't understand exactly how dynamic do you want to be. Could you explain further what would be the objective?

As for the overloads. Do you mean like the singledispatch? Could you perhaps give an example?

Overloads in python typing work like this:

from typing import overload

# Define the overload signatures.
@overload
def sum(a:int, b:int) -> int:
    ...

@overload
def sum(a:float, b:float) -> float:
    ...

# And finally define the real function
def sum(a: int | float, b: int | float) -> int | float:
    return a + b

So you don't need to duplicate the code of the function, but you need to repeat the signature.

Probably we should think of ways of standardising return types... I. E. Other ways than having as_* arguments... Any ideas?

Hmm the first thing that comes to mind is to define functions that can return multiple types as dispatchers, but I don't know if this would be a great idea for usability. E.g.:

# Returns the default type
bands_sile.read_data(...)

# Forces the return of an array
bands_sile.read_data.array(...)

# Forces the return of a DataArray
bands_sile.read_data.dataarray(...)

pfebrer avatar Mar 27 '22 11:03 pfebrer

Ah great with the overloads. Yes! So long as code + doc isn't duplicated, I am fine!

Yeah, I also thought about dispatchers... But perhaps let's wait with that.

zerothi avatar Mar 27 '22 11:03 zerothi

We can safely close this as it has been agreed upon, PR's for typing is very much welcome!

zerothi avatar Feb 21 '24 22:02 zerothi