Skip to content

TYP: check_untyped_defs io.sas.sasreader #30659

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

Conversation

simonjayhawkins
Copy link
Member

pandas\io\sas\sasreader.py:75: error: Incompatible types in assignment (expression has type "SAS7BDATReader", variable has type "XportReader")

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Jan 3, 2020
@@ -63,14 +71,15 @@ def read_sas(
else:
raise ValueError("unable to infer format of SAS file")

reader: Union["XportReader", "SAS7BDATReader"]
Copy link
Member

Choose a reason for hiding this comment

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

Do these have a shared base class that is directly importable? Seems like this requires a bit of import gymnastics not sure worth it

Copy link
Member Author

Choose a reason for hiding this comment

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

they share abc.Iterable ...

pandas\io\sas\sasreader.py:93: error: "Iterator[Any]" has no attribute "read"
pandas\io\sas\sasreader.py:94: error: "Iterator[Any]" has no attribute "close"

many options:

Use protocol (python 3.8) from typing_extensions
define abc base class with read and close methods (add comment to use Protocol from py3.8)
use dynamically typed code, i.e. reader: Any
use ignore
leave as is with import gymnastics and Union.

we normally try to avoid most of these.

Copy link
Member Author

Choose a reason for hiding this comment

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

define abc base class with read and close methods (add comment to use Protocol from py3.8)

gone with this option.

@jreback jreback added this to the 1.0 milestone Jan 4, 2020

def read_sas(
filepath_or_buffer,
format=None,
filepath_or_buffer: FilePathOrBuffer[AnyStr],
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should change the definition in _typing?

Copy link
Member Author

Choose a reason for hiding this comment

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

can you elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

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

FilePathOrBuffer[AnyStr] should be in _typing instead of just the not parametric FilePathOrBuffer

Copy link
Member Author

Choose a reason for hiding this comment

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

for some read/write methods only a string buffer (or bytes buffer) is allowed, i.e. to_html and we need to annotate as FilePathOrBuffer[str] (or FilePathOrBuffer[bytes]) otherwise we get mypy errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

that’s a very limited case compared to the more general which accepts strings and buffers

these should be distinct / parametrized

not just in this PR - this is my point

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure I understand, AnyStr is a TypeVar and therefore parametrized https://docs.python.org/3/library/typing.html#typing.AnyStr

Copy link
Contributor

Choose a reason for hiding this comment

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

so, you are adding this here, rather than as a generailized soln in _typing. I would think it would be better there than adding to each of the IO readers.

Copy link
Member Author

Choose a reason for hiding this comment

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

i reverted these additions for now to keep this PR focused on the untyped_defs error, #30659 (comment)

from pandas.io.common import stringify_path

if TYPE_CHECKING:
from pandas.io.sas.sas_xport import XportReader # noqa: F401
Copy link
Contributor

Choose a reason for hiding this comment

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

acutally i think you can just import both readers at the top and call it a day.

@jreback
Copy link
Contributor

jreback commented Jan 5, 2020

rebase as well

@jreback jreback removed this from the 1.0 milestone Jan 9, 2020
@simonjayhawkins
Copy link
Member Author

need to sort out some import issues. closing to reduce queue for now

@jreback jreback added this to the 1.1 milestone Apr 26, 2020
@jreback
Copy link
Contributor

jreback commented Apr 26, 2020

@simonjayhawkins if you can merge master and merge on green.

@simonjayhawkins simonjayhawkins merged commit 25fe9fc into pandas-dev:master Apr 27, 2020
@simonjayhawkins simonjayhawkins deleted the pandas.io.sas.sasreader branch April 27, 2020 08:27
rhshadrach pushed a commit to rhshadrach/pandas that referenced this pull request May 10, 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.

3 participants