-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Allow mypy to flag str
matching against Sequence[str]
as an error
#11001
Comments
Linking #5090 |
Any plans on implementing this? It bites me semi-reliably and the pytype approach seems to be working out. |
As the OP on this, I'll note that I've changed my perspective a bit in the time since opening this. Without this being a part of the type system with some support, library maintainers run into trouble. It becomes unclear what types should be published if, for example, an API is meant to take any There's currently an ongoing thread on discuss.python.org which explores this a bit. Some folks in that thread want to put this idea into I think this is a valuable linting check. But if it's not in the type system's strict rules, it's not really a type-check anymore. It's hard to say that code which passes a This whole situation reminds me of some of |
In my opinion the problem is that Technically these statements are true, but semantically most often not. The perhaps ~99% use case of a Regarding the rare use cases that actually want a sequence of characters: Since this can be easily solved explicitly via |
When make_vol_opt() was called with options=[], it would return a volume spec which erroneously contained a trailing colon. Also, this removes the unnecessary flexibility of passing a single option string (as opposed to a sequence of option strings). This wasn't intended by the type signature for options, but unforunately a 'str' is a 'Sequence[str]'. See python/mypy#11001. This fixes #215.
I've added a |
What would it take to implement this? I have yet to see any strong arguments against adding an optional flag, and |
Feature
A new optional setting for mypy which forbids matching
str
againstSequence[str]
orIterable[str]
. This will catch a wide variety of bugs which users encounter when defining APIs which consume these types and are unintentionally passed strings. It also allows correct overloading ofstr
vsSequence[str]
to describe behavior.The option would require the explicit (but "redundant")
Union[str, Sequence[str]]
orUnion[str, Iterable[str]]
for values which can be a string or non-string iterable of strings.Pitch
python/typing#256 has been an open issue for a while, with no clear resolution within the type system itself.
However the pytype maintainers recently weighed in to mention that they took this approach, forbidding
str
from matchingSequence[str]
, and their users have been happy with the results. I think mypy should match this behavior if possible/feasible.In answer to Guido's question about whether or not this should apply to
AnyStr
, the answer is "ideally yes". However, if that's significantly harder, only applying tostr
is probably fine:str
is likely the most common case for python developers.Use Cases
The text was updated successfully, but these errors were encountered: