-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add recommendation to use string literal escaping #7707
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
Conversation
mypy/typeanal.py
Outdated
@@ -907,8 +911,13 @@ def get_omitted_any(disallow_any: bool, fail: FailCallback, | |||
typ = unexpanded_type or typ | |||
type_str = typ.name if isinstance(typ, UnboundType) else format_type_bare(typ) | |||
|
|||
fail(message_registry.BARE_GENERIC.format(quote_type_string(type_str)), typ, | |||
code=codes.TYPE_ARG) | |||
if type_str in GENERIC_STUB_NOT_AT_RUNTIME_TYPES: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what's special about OrderedDict. The same thing would happen with any type that's defined both in collections and in typing. And in general I'm not sure that this particular hint belongs in the error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JukkaL wanted this little reminder in the error message:
#7539 (comment)
Indeed OrderedDicts are not unique that way, Queue would also have to go into the list.
This error message is useful because OderedDict[Any, Any] would pass mypy checks atm. but wouldn’t run. Many users would probably have difficulties understanding the problem and finding a fix.
So this is purely for usability.
Couldn’t we just check this dynamically with getattr(typ, "__getitem__", None) |
But I believe you can use ‘from typing import OrderedDict’ and the problem will go away. Is that not so? |
No, this would indeed run(runtime), but raises a mypy error: from typing import OrderedDict
from typing import Any
def name(value: OrderedDict[Any, Any]) -> None:
pass The error it raises when mypy checked:
|
This is just a bug that should be fixed by updating |
Should this little reminder still be added or not, I just saw the 'good-first-issue' by JukkaL and wanted to contribute. I think what this boils down to is just usability for new users, like me.
I could make a new pr with this fix, or just add it to this one :) |
I think it still makes sense, but:
|
Would you add a new doc page or just make the name shorter?
My PR introduces such a list, I will remove OD`s |
I think a separate PR is better
I am not a big expert in Sphinx, so I hope there is a way to generate a shorter link without changing the name. If not then you can try finding a better shorter title for that section. |
This has the note working and a shorter link to the doc I have tested this with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates! This looks almost ready, I have few more comments.
@@ -533,6 +533,7 @@ Here's the above example modified to use ``MYPY``: | |||
def listify(arg: 'bar.BarClass') -> 'List[bar.BarClass]': | |||
return [arg] | |||
|
|||
.. _not-generic-runtime: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading now through this section I think it makes sense to add couple sentences at the end about using from __future__ import annotations
as a (better) alternative to string quotes on Python 3.7+.
mypy/typeanal.py
Outdated
@@ -55,6 +55,10 @@ | |||
'mypy_extensions.KwArg': ARG_STAR2, | |||
} # type: Final | |||
|
|||
GENERIC_STUB_NOT_AT_RUNTIME_TYPES = { | |||
'Queue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add os.PathLike
here.
mypy/typeanal.py
Outdated
@@ -892,9 +902,10 @@ def tuple_type(self, items: List[Type]) -> TupleType: | |||
|
|||
# Mypyc doesn't support callback protocols yet. | |||
FailCallback = Callable[[str, Context, DefaultNamedArg(Optional[ErrorCode], 'code')], None] | |||
NoteCallback = Callable[[str, Context, DefaultNamedArg(Optional[ErrorCode], 'code')], None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alias is identical to the above one. You can rename it to MsgCallback
and use it to annotate both fail
and note
.
mypy/typeanal.py
Outdated
typ, | ||
code=codes.TYPE_ARG) | ||
|
||
if type_str in GENERIC_STUB_NOT_AT_RUNTIME_TYPES: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not safe to make this decision using type_str
, you should use fullname
like few lines above.
Implemented all things as requested (hopefully) :) |
mypy/typeanal.py
Outdated
note( | ||
"Subscripting classes that are not generic at runtime may require " | ||
"escaping. If you are running Python 3.7+ you can simply use " | ||
"`from __future__ import annotations`, otherwise please " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually wanted to add this to the docs themselves, not here. Please keep the note as it was, and instead add this sentence (plus maybe also a link to PEP 563) at the end of the docs section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updates! I pushed couple small tweaks, will merge this soon.
Do you still want to make a PR for the |
I can make it tomorrow if you want :) |
OK, good. |
This commit introduces a recommendation for classes that don't support indexing at runtime to be escaped as string literals on missing required generic type args (BARE_GENERIC) error.
It should resolve #7539
This is my first time contributing here, so please let me know if there is anything I could have done better. :)