-
Notifications
You must be signed in to change notification settings - Fork 998
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
Check longitude and latitude are numbers (float or integer) #1319
Conversation
Hi @ndclt, thanks for the PR, and answering the stackoverflow question :) I'm not sure what I think about type validation here. In general pvlib-python doesn't do much if any type validation and relies on documenting the expected types in the docstring (see e.g. If we do add this, I think the error message should include some more detail like |
+1 I'm -1 on type validation but +1 on type annotations for the |
Hi @kanderso-nrel and @wholmgren, I'm not an regular user of pvlib (I discovered it with the question on stackoverflow and by the way, it was really easy to install it and test it before this PR). So, if you don't agree with this kind of check, I won't strongly push for it. I could easily do a PR adding some type annotation to the class Location
def __init__(self, latitude: float, longitude: float, tz: Union[str, datetime.timezone, datetime.tzinfo, int, float]='UTC', altitude: float=0, name: Optional[str]=None):
.... but as from what I see, there is any type in this library and that would be a really small begin. I never migrate an existing code base to annotation. I found this tips from the mypy checker. I also give a try to the monkeytype package in this branch. Would you like me to create a PR with these modifications? I answer your questions below (but more for the record than changing your mind).
I was thinking so because of the side effect. It's not so easy to guess that a 6 depth stacktrace is coming from a wrong given type when creating the The full stacktrace of the stackoverflow question
Yes I think we should. |
I'm also -1 on type validation and neutral about using type annotations, mostly because I don't really understand them and am not clear what advantage they provide over the docstring, other than the convenience of raising hints in an IDE. |
I previously wrote type hints for Some in the pvlib community are not enthusiastic about type hints, so to get started I would not annotate the other methods. Simply annotating the object constructors/attributes should catch a fair number of problems.
To me they are most helpful when the IDE is setup to flag/underline conflicts as I write/review code. Most of the pvlib function layer is straightforward enough, but the object layer could benefit from hints. I don't recall the details, but the In any case, it seems there's not an appetite for type checking these inputs. I'd be happy for help with experimenting with type annotations in the |
From all the comments I get, I close the pull request. Thanks to all of you for the review. |
docs/sphinx/source/api.rst
for API changes. (not relevant for this MR)docs/sphinx/source/whatsnew
for all changes. Includes link to the GitHub Issue with:issue:`num`
or this Pull Request with:pull:`num`
. Includes contributor name and/or GitHub username (link with:ghuser:`user`
). I didn't find a file for the unreleased version where I can add my modificationContext
In this Stackoverflow question, the OP creates a
Location
object with strings without errors and this leads to a Numpy error when using the functionget_solarposition
. Numpy couldn't add a string with two float array. The string was theobserver_longitude
directly given from thelongitude
attribute of aLocation
instance.Goal
This PR aims at raising an error when something different from a number is given to the
Location
init function as longitude or latitude argument.Implementation choice
I hesitate between the solution proposed in the PR and something like this:
I prefer the PR version because it doesn't hide the conversion (which is done with the
float
use) but the PR version won't work with a size-1 numpy array whereas thefloat
one will (but I don't know if it's a common use case).