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

TYPING: more type hints for io.formats.printing #27765

Merged
merged 5 commits into from
Aug 23, 2019

Conversation

simonjayhawkins
Copy link
Member

xref #27568

$ mypy pandas/io/formats/printing.py --check-untyped-defs --follow-imports skip
pandas\io\formats\printing.py:194: error: "str" has no attribute "decode"; maybe "encode"?

@simonjayhawkins simonjayhawkins added 2/3 Compat Typing type annotations, mypy/pyright type checking labels Aug 5, 2019
result = str(thing).decode("utf-8", "replace")

def as_escaped_string(
thing: object, escape_chars: Optional[EscapeChars] = escape_chars
Copy link
Member

Choose a reason for hiding this comment

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

Can you use Any instead of object? Won't change output since this is all green but would be more idiomatic

Copy link
Member Author

Choose a reason for hiding this comment

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

Contrast the behavior of Any with the behavior of object. Similar to Any, every type is a subtype of object. However, unlike Any, the reverse is not true: object is not a subtype of every other type.

That means when the type of a value is object, a type checker will reject almost all operations on it, and assigning it to a variable (or using it as a return value) of a more specialized type is a type error.

Use object to indicate that a value could be any type in a typesafe manner. Use Any to indicate that a value is dynamically typed.

https://docs.python.org/3/library/typing.html#the-any-type

object is used here to indicate that a value could be any type in a typesafe manner and reject almost all operations on it.

Copy link
Member Author

@simonjayhawkins simonjayhawkins Aug 7, 2019

Choose a reason for hiding this comment

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

In the outer function, thing should be Any since it is dynamically typed. we used hasattr and isinstance introspection. But following previous discussions, refraining from using Any and just leaving untyped.

@WillAyd WillAyd added this to the 1.0 milestone Aug 7, 2019
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 @jreback

@WillAyd
Copy link
Member

WillAyd commented Aug 23, 2019

@simonjayhawkins can you merge master?

@simonjayhawkins
Copy link
Member Author

@simonjayhawkins can you merge master?

done.

@WillAyd WillAyd merged commit e2483c0 into pandas-dev:master Aug 23, 2019
@WillAyd
Copy link
Member

WillAyd commented Aug 23, 2019

Thanks!

@simonjayhawkins simonjayhawkins deleted the io.formats.printing branch August 24, 2019 07:45
galuhsahid pushed a commit to galuhsahid/pandas that referenced this pull request Aug 25, 2019
galuhsahid added a commit to galuhsahid/pandas that referenced this pull request Aug 25, 2019
* master: (40 commits)
  DOC: Fix GL01 and GL02 errors in the docstrings (pandas-dev#27988)
  Remove Encoding of values in char** For Labels (pandas-dev#27618)
  TYPING: more type hints for io.formats.printing (pandas-dev#27765)
  TST: fix compression tests when run without virtualenv/condaenv (pandas-dev#28051)
  DOC: Start 0.25.2 (pandas-dev#28111)
  DOC: Fix docstrings lack of punctuation (pandas-dev#28031)
  DOC: Remove alias for numpy.random.randn from the docs (pandas-dev#28082)
  DOC: update GroupBy.head()/tail() documentation (pandas-dev#27844)
  BUG: timedelta merge asof with tolerance (pandas-dev#27650)
  BUG: Series.rename raises error on values accepted by Series construc… (pandas-dev#27814)
  Preserve index when setting new column on empty dataframe. (pandas-dev#26471)
  BUG: Fixed groupby quantile for listlike q (pandas-dev#27827)
  BUG: iter with readonly values, closes pandas-dev#28055 (pandas-dev#28074)
  TST: non-strict xfail for period test (pandas-dev#28072)
  DOC: Update whatsnew (pandas-dev#28073)
  CI: disable codecov (pandas-dev#28065)
  CI: Set SHA for codecov upload (pandas-dev#28067)
  BUG: Correct the previous bug fixing on xlim for plotting (pandas-dev#28059)
  CI: Add pip dependence explicitly (pandas-dev#28008)
  DOC: Change document code prun in a row (pandas-dev#28029)
  ...
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
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.

2 participants