Skip to content

TYP: Make mypy 0.800 compatible #39407

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 2 commits into from
Jan 26, 2021
Merged

Conversation

phofl
Copy link
Member

@phofl phofl commented Jan 25, 2021

@phofl phofl added the Typing type annotations, mypy/pyright type checking label Jan 25, 2021
@jbrockmendel
Copy link
Member

LGTM cc @simonjayhawkins

environment.yml Outdated
@@ -23,7 +23,7 @@ dependencies:
- flake8
- flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions
- isort>=5.2.1 # check that imports are in the right order
- mypy=0.790
- mypy
Copy link
Contributor

Choose a reason for hiding this comment

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

i think ok to pin >=

Copy link
Member

Choose a reason for hiding this comment

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

we have been pinning to exact version up to now.

we pin to avoid ci surprises. also since we use warn_unused_ignores = True, mypy will only be green with the specified version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx, pinned again

@@ -28,7 +28,7 @@ class BoxPlot(LinePlot):

_valid_return_types = (None, "axes", "dict", "both")
# namedtuple to hold results
BP = namedtuple("Boxplot", ["ax", "lines"])
BP = namedtuple("BP", ["ax", "lines"])
Copy link
Contributor

Choose a reason for hiding this comment

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

OT we could use data classes here

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Jan 26, 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 @phofl generally lgtm

environment.yml Outdated
@@ -23,7 +23,7 @@ dependencies:
- flake8
- flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions
- isort>=5.2.1 # check that imports are in the right order
- mypy=0.790
- mypy
Copy link
Member

Choose a reason for hiding this comment

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

we have been pinning to exact version up to now.

we pin to avoid ci surprises. also since we use warn_unused_ignores = True, mypy will only be green with the specified version.

@@ -182,7 +182,7 @@ def stringify_path(
return cast(FileOrBuffer[AnyStr], filepath_or_buffer)

# Only @runtime_checkable protocols can be used with instance and class checks
Copy link
Member

Choose a reason for hiding this comment

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

can remove 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.

Done

@@ -2491,9 +2491,9 @@ def write_file(self) -> None:
self.handles.close()
# Only @runtime_checkable protocols can be used with instance and class
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -79,7 +79,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]:
{dedent(doc)}"""
)

return wrapper
return wrapper # type: ignore[return-value]
Copy link
Member

Choose a reason for hiding this comment

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

add the error message as a comment to help future readers and contributors looking to help with #37715

Suggested change
return wrapper # type: ignore[return-value]
# error: Incompatible return value type (got "Callable[[VarArg(Any),
# KwArg(Any)], Callable[...,Any]]", expected "Callable[[F], F]")
return wrapper # type: ignore[return-value]

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -330,8 +331,9 @@ def test_read_csv_file_handle(all_parsers, io_class, encoding):
parser = all_parsers
expected = DataFrame({"a": [1], "b": [2]})

content = "a,b\n1,2"
content: Union[str, bytes] = "a,b\n1,2"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
content: Union[str, bytes] = "a,b\n1,2"
content: str | bytes = "a,b\n1,2"

😁

we have

[mypy-pandas.tests.*]
check_untyped_defs=False

so not sure why mypy is checking this function anyway.

OTOH could refactor...

    content = "a,b\n1,2"
    handle = io_class(content.encode("utf-8") if io_class == BytesIO else content)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm the refactor is better. Thx.

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 @phofl lgtm pending green

@phofl
Copy link
Member Author

phofl commented Jan 26, 2021

@simonjayhawkins green.

Could you point me to an example of something like str | bytes? Searched for something like this but could not find it

@simonjayhawkins
Copy link
Member

Could you point me to an example of something like str | bytes? Searched for something like this but could not find it

from http://mypy-lang.blogspot.com/

Two new Python features improve this situation and are now supported by mypy:

PEP 585 lets you use list[int] instead of List[int] (no need to import List and other generic collections from typing).
PEP 604 lets you write X | Y instead of Union[X, Y], and X | None instead of Optional[X] (no need to import Union or Optional from typing).
Note: Using list[int] requires Python 3.9 and X | Y requires Python 3.10 (alpha) in order to work at runtime. To use them on older versions of Python, use from __future__ import annotations. This allows them to be used in type annotations, but the older variants (or string literal escaping) may be required in non-annotation contexts, such as in type aliases. See the docs for more details.

@simonjayhawkins simonjayhawkins merged commit 2c4c9f3 into pandas-dev:master Jan 26, 2021
@phofl
Copy link
Member Author

phofl commented Jan 26, 2021

thanks very much, should have looked there and not in our codebase :)

@phofl phofl deleted the typ_mypy_0800 branch January 26, 2021 20:47
@simonjayhawkins
Copy link
Member

@phofl also need to update the version in the doc/source/whatsnew/v1.3.0.rst

@phofl
Copy link
Member Author

phofl commented Jan 26, 2021

Thx, did not think about this. #39419

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.

TYP upgrade and pin mypy to 0.800
4 participants