torchgeo icon indicating copy to clipboard operation
torchgeo copied to clipboard

Adds custom typehinting for size (pixel or CRS) arguments

Open TCherici opened this issue 3 years ago • 1 comments

My IDE was complaining about me using an integer for the size parameter, despite that being not only supported, but also the default expected type.

I'm not 100% certain about the naming of the custom type (PixelsOrCRS), or if its addition even hinders readability, but explicitly writing Union[int, float] would make the type hinting very verbose and difficult to read imo.

TCherici avatar Jul 27 '22 08:07 TCherici

I think Number would be a better name for Union[int, float].

But more importantly, this seems like a bug in your IDE, not a bug in our type hints. According to PEP 484:

Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument of type int is acceptable

What IDE do you use? Maybe it would be more productive to open an issue with them instead. This doesn't just affect torchgeo.samplers, it affects all Python libraries that use type hints.

adamjstewart avatar Jul 27 '22 17:07 adamjstewart

What IDE do you use? Maybe it would be more productive to open an issue with them instead. This doesn't just affect torchgeo.samplers, it affects all Python libraries that use type hints.

@TCherici?

adamjstewart avatar Aug 11 '22 23:08 adamjstewart

Closing as this is a bug in the IDE, not a bug in TorchGeo or mypy.

adamjstewart avatar Aug 22 '22 06:08 adamjstewart

Note that a slight change was made to these type hints in #876. Float is more general than int. If ints are input, ints are also returned, not floats.

adamjstewart avatar Jan 24 '23 19:01 adamjstewart