Skip to content
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

TYP: misc typing in core\indexes\base.py #35991

Merged
merged 3 commits into from
Sep 2, 2020

Conversation

simonjayhawkins
Copy link
Member

No description provided.

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Aug 30, 2020


str_ = str
Copy link
Member

Choose a reason for hiding this comment

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

i think elsewhere we use "str_t" for this

Copy link
Member Author

Choose a reason for hiding this comment

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

we use both str_t and str_type so why no have another?

only jesting. see #32365 (comment)

will change to str_t for now.

@@ -5516,7 +5534,9 @@ def ensure_index_from_sequences(sequences, names=None):
return MultiIndex.from_arrays(sequences, names=names)


def ensure_index(index_like, copy: bool = False):
def ensure_index(
index_like: Union[AnyArrayLike, Sequence], copy: bool = False
Copy link
Member

Choose a reason for hiding this comment

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

does Sequence not subsume ArrayLike?

Copy link
Member Author

Choose a reason for hiding this comment

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

see #28770

def slice_indexer(
self,
start: Optional[Label] = None,
end: Optional[Label] = None,
Copy link
Member

Choose a reason for hiding this comment

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

isnt Optional redundant here?

Copy link
Member Author

Choose a reason for hiding this comment

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

we've got no_implicit_optional=True in out setup.cfg (but I don't know why?)

it would work, but only since we have Label = Optional[Hashable] to allow None to be a Label. Label would be easier to grok if it were Union[Hashable, None] (or if Hashable included None) but for consistency we use Optional.

Here, for consistency of keyword parameter annotations, don't really want to remove the Optional, even though already accounted for in Label.

but I'll change if blocker.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm

@simonjayhawkins simonjayhawkins added this to the 1.2 milestone Sep 1, 2020
@@ -188,6 +190,9 @@ def _new_Index(cls, d):
return cls.__new__(cls, **d)


_IndexT = TypeVar("_IndexT", bound="Index")
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment before this (and in other cases where we use TypeVar like this); also pls confirm that the ref is consistent (e.g. FrameOrSeries is different )

Copy link
Member Author

Choose a reason for hiding this comment

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

TL;DR lmk explictly what you want to name the TypeVar otherwise i'll leave it as is for now pending further discussion.


also pls confirm that the ref is consistent

we are far from consistent, the use of TypeVar outside of pandas._typing is

_T = TypeVar("_T", bound="NDArrayBackedExtensionArray") added in #33660

DatetimeLikeArrayT = TypeVar("DatetimeLikeArrayT", bound="DatetimeLikeArrayMixin") added in #33706

BaseMaskedArrayT = TypeVar("BaseMaskedArrayT", bound="BaseMaskedArray") added in #31728

_T = TypeVar("_T", bound="BaseExprVisitor") added in #31365

ScalarResult = TypeVar("ScalarResult")

OutputFrameOrSeries = TypeVar("OutputFrameOrSeries", bound=NDFrame) added in #33286

_T = TypeVar("_T", bound="DatetimeIndexOpsMixin") added in #33839

T = TypeVar("T", bound="BlockManager") added in #32421

DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime)

and a couple of uses of
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")

my preference is _<classname>T, i.e. leading underscore followed by the class name passed as the bound argument followed by a uppercase T to indicate TypeVar. (of course where a union is used instead of a bound this allows for more imaginative naming)

In pandas._typing, the TypeVars are imported by other modules, so we don't use leading underscores

see also https://github.com/numpy/numpy/blob/3fbc84a5662ffd985a071b0bbdcd59e655041ad3/numpy/__init__.pyi for other ideas on naming.

we could add Self suffix instead of T for TypeVars used to preserve return types or we could drop the T altogether. so using that naming convention, we would change the _IndexT added here to _IndexSelf since this TypeVar is used to maintain the return type of .copy()

for abstract/base classes we could add SubClass suffix (so FrameOrSeries could be NDFrameSubClass)

e.g. FrameOrSeries is different

FrameOrSeries was originally

FrameOrSeries = TypeVar("FrameOrSeries", "Series", "DataFrame")

before being changed in #28173 to

FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame")

can you add a comment before this (and in other cases where we use TypeVar like this)

TypeVar is a fundamental building block[1] of typing and if we are consistent with the naming, additional comments explaining fundamental use of typing shouldn't be necessary.

[1] from https://www.python.org/dev/peps/pep-0484/

Fundamental building blocks:

  • Any, used as def get(key: str) -> Any: ...
  • Union, used as Union[Type1, Type2, Type3]
  • Callable, used as Callable[[Arg1Type, Arg2Type], ReturnType]
  • Tuple, used by listing the element types, for example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.)
  • TypeVar, used as X = TypeVar('X', Type1, Type2, Type3) or simply Y = TypeVar('Y') (see above for more details)
  • Generic, used to create user-defined generic classes
  • Type, used to annotate class objects

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah as long as consistent doesn't matter much, IndexT looks good to me: importable (no leading _), not too crazy

Copy link
Contributor

Choose a reason for hiding this comment

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

obviously can do this in a dedicated PR

@jreback jreback merged commit caca6e1 into pandas-dev:master Sep 2, 2020
@jreback
Copy link
Contributor

jreback commented Sep 2, 2020

thanks @simonjayhawkins

@simonjayhawkins simonjayhawkins deleted the indexes.base branch September 2, 2020 18:49
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants