PlasmaPy icon indicating copy to clipboard operation
PlasmaPy copied to clipboard

Improve consistency in type hint annotations

Open namurphy opened this issue 8 years ago • 7 comments

Right now, we have a mix of type hint annotation styles. Sometimes we import the packages:

import typing
import numbers

def f(x: typing.Optional[numbers.Real]):
    ...

Other times, we import the classes from the packages:

from typing import Optional
from numbers import Real

def f(x: Optional[Real]):
    ...

Importing the packages has the advantage of showing the full namespace. Importing the classes from the packages has the advantage of improving readability.

I propose that we standardize our type hints by using the second example above and importing the classes from the packages before using them in annotations. I prefer the second method because it prioritizes readability, and there's very little chance of local name clashes. Most of the time, the annotations are only being used to annotate (with the exception of annotations like Optional[Particle] when used with @particle_input).

Whichever we decide, we should apply this consistently throughout PlasmaPy, and describe the preferred import style in the development guide.

namurphy avatar Oct 23 '18 18:10 namurphy

Also, from the PEP 8 style guide discussion of imports:

When importing a class from a class-containing module, it's usually okay to spell this:

from myclass import MyClass
from foo.bar.yourclass import YourClass

If this spelling causes local name clashes, then spell them explicitly:

import myclass
import foo.bar.yourclass

and use myclass.MyClass and foo.bar.yourclass.YourClass.

namurphy avatar Oct 23 '18 18:10 namurphy

There was general support for this in our community meeting today, so let's go for it and make these changes. :slightly_smiling_face:

namurphy avatar Oct 30 '18 19:10 namurphy

@namurphy I could jump on this, I'd like to get involved with the project :)

Jac0bDeal avatar Nov 30 '18 04:11 Jac0bDeal

For type hints such as u.kg, u.K, or np.nan, should they be updated to match the consistency? I could see arguments going both ways, namely that the u and np in these cases actually increase readability by specifying the source.

Jac0bDeal avatar Nov 30 '18 04:11 Jac0bDeal

@Jac0bDeal go for it! :) Let's definitely keep variables such as u.kg and np.nan the way they are, though - you're quite right about better readability there, there is such a thing as too short a variable name. :D

StanczakDominik avatar Nov 30 '18 05:11 StanczakDominik

@StanczakDominik Cool, thanks! I've also noted some missing type hints, but I'm leaving them untouched because I'd rather lean towards keeping the PR on just this topic. Should I make note of the missing type hints and create a new issue for adding them?

Also thanks for the quick reply! I'm excited to join the project as I haven't gotten to work on much physics since graduating a couple years back. :)

Jac0bDeal avatar Nov 30 '18 05:11 Jac0bDeal

Either a new issue or going a little bit out of issue scope along the way is completely fine. :smiley:

The pleasure is mine! It's nice to see you're interested :)

StanczakDominik avatar Nov 30 '18 06:11 StanczakDominik