Skip to content

TYP: overload lib.maybe_convert_objects #41166

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

Merged
merged 3 commits into from
Apr 28, 2021

Conversation

jbrockmendel
Copy link
Member

No description provided.

@MarcoGorelli MarcoGorelli self-requested a review April 26, 2021 20:01
@jreback jreback added the Typing type annotations, mypy/pyright type checking label Apr 26, 2021
@MarcoGorelli MarcoGorelli self-requested a review April 27, 2021 14:41
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

There's a part (which I've commented on) where I haven't understood why mypy is reporting an error - on than that, this looks good to me

Comment on lines 64 to 69
# error: Argument 1 to "ndarray" has incompatible type "int";
# expected "Sequence[int]"
return np.ndarray(0, dtype=dtype) # type: ignore[arg-type]
# error: Argument "dtype" to "array" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[object]]"; expected
# "Union[dtype[Any], None, type, _SupportsDType, str,
# Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any],
# _DTypeDict, Tuple[Any, Any]]]"
return np.array([], dtype=dtype) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused about why mypy reports this, if I try making a file t.py with

import numpy as np
from pandas._typing import Dtype, Optional

def foo(dtype: Optional[Dtype] = None):
    if dtype is None:
        dtype = np.dtype("object")
    return (np.array([], dtype=type))

then mypy type checks it just fine. Do you know what I might be missing?

Copy link
Member Author

Choose a reason for hiding this comment

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

no idea. i think this edit is unrelated to most of the rest; using np.ndarray here instead of np.array weirds me out

Copy link
Member

Choose a reason for hiding this comment

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

The Dtype alias includes ExtensionDtype which cannot be passed on to numpy

>>> np.array([], dtype=pd.Int64Dtype())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot interpret 'Int64Dtype()' as a data type
>>> 

Do you know what I might be missing?

return (np.array([], dtype=type)) -> return (np.array([], dtype=dtype))

Copy link
Member

Choose a reason for hiding this comment

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

I'm embarrassed, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

so this looks like there maybe an actual bug here. As part of the ArrowStringArray work, I have been parameterising existing tests on object dtype arrays with StringArray and ArrowStringArray and this has maybe uncovered some latent bugs with StringArray.

I think OK to leave this ignore as a 'fix later' and out of scope here.

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel generally lgtm

new_values, _ = func(
self.to_numpy(object), # type: ignore[arg-type]
self.to_numpy("object"),
Copy link
Member

Choose a reason for hiding this comment

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

This is an unrelated change to the scope (from the PR title) of this PR. The mypy error is a false positive, no need to change code. will be fixed in #41185

but I guess nbd (other than merge conflicts)

Comment on lines 64 to 69
# error: Argument 1 to "ndarray" has incompatible type "int";
# expected "Sequence[int]"
return np.ndarray(0, dtype=dtype) # type: ignore[arg-type]
# error: Argument "dtype" to "array" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[object]]"; expected
# "Union[dtype[Any], None, type, _SupportsDType, str,
# Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any],
# _DTypeDict, Tuple[Any, Any]]]"
return np.array([], dtype=dtype) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

so this looks like there maybe an actual bug here. As part of the ArrowStringArray work, I have been parameterising existing tests on object dtype arrays with StringArray and ArrowStringArray and this has maybe uncovered some latent bugs with StringArray.

I think OK to leave this ignore as a 'fix later' and out of scope here.

# error: Incompatible types in assignment (expression has type "None", variable has
# type "ExtensionArray")
result = None # type: ignore[assignment]
result = None
Copy link
Member

Choose a reason for hiding this comment

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

is this needed? The if result is None will always be True?

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Apr 28, 2021
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel lgtm

@simonjayhawkins simonjayhawkins merged commit 59b2db1 into pandas-dev:master Apr 28, 2021
@jbrockmendel jbrockmendel deleted the typ-cln branch April 28, 2021 19:44
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
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.

4 participants