question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TYP: Remove Sequence[str] in type annotations

See original GitHub issue

In various places, we use Sequence[str] as a type annotation. The problem is that a regular string also matches Sequence[str] . So we need to look at the various places we use Sequence[str] and change the type accordingly. Maybe just List or ArrayLike, dependent on the context.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
Dr-Irvcommented, Jun 5, 2022

Mostly it is this:

the implementation doesn’t actually work with a “sequence of characters”?

So we document a method like DataFrame.to_string() with columns and header being a list of columns/names, and type it as Sequence[str], but you can’t just pass a single string (it would match for typing). What we want to accept there is something array-like, but not a plain string.

0reactions
Dr-Irvcommented, Jun 6, 2022
  • Should this be the case for only the public API or are also for the internal code? If it is only the public code: should it be pandas and pandas-stubs?

For pandas-stubs, I’m taking out all references to Sequence[str] and replacing it with List[str]. Maybe that will be too narrow, but if someone has an argument x for some method that is a sequence and not a list, they could just do list(x) and the type checker will be happy.

I think the debate here is more about what we do internally in the code. For the PR’s I submitted, narrowing the type to List[str] still passes the mypy type checking.

The other issue is more of a documentation one, where we are not necessarily precise in the meanings of list-like, array-like. I think that many times where we write in the docs array-like, we mean 1-D array-like, as we wouldn’t accept a higher dimensional array for those arguments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3 - type hints for sequences of str - Stack Overflow
You should use the Any Type if you are not sure from typing import Any def f(a: Sequence[Any], b: Sequence[Any]) -> List[Any]: return ......
Read more >
Common issues and solutions - mypy 0.991 documentation
This section has examples of cases when you need to update your code to use static typing, and ideas for working around issues...
Read more >
Python Type Checking (Guide) - Real Python
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
Introducing syntax for annotating variables outside of function ... The deprecated types will be removed from the typing module in the first Python...
Read more >
Using Python's Type Annotations - DEV Community ‍ ‍
from typing import Sequence def print_names(names: Sequence[str]) ... from typing import List, Tuple # Declare a point type annotation using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found