-
-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e30bea6
Add recommendation to use string literal escaping
Coronon 3983f50
PEP8 compliant
Coronon 56de035
PEP8 compliant2
Coronon 5455ea4
Changed recom to note and added shorter doc anchor
Coronon 75ff95d
fixed `multiple values for argument 'note'` + PEP8
Coronon 45e12fb
annotations, MsgCallback, os.Pathlike, fullname
Coronon 3ededa1
moved annotations to docs
Coronon 98697b8
Tweak docs
e7ea063
Add test (and fix it)
5a1245d
Fix test on Python 3.5
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,11 @@ | |
'mypy_extensions.KwArg': ARG_STAR2, | ||
} # type: Final | ||
|
||
GENERIC_STUB_NOT_AT_RUNTIME_TYPES = { | ||
'queue.Queue', | ||
'os.Pathlike', | ||
} # type: Final | ||
|
||
|
||
def analyze_type_alias(node: Expression, | ||
api: SemanticAnalyzerCoreInterface, | ||
|
@@ -221,8 +226,13 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool) | |
res = get_proper_type(res) | ||
if (isinstance(res, Instance) and len(res.args) != len(res.type.type_vars) and | ||
not self.defining_alias): | ||
fix_instance(res, self.fail, disallow_any=disallow_any, use_generic_error=True, | ||
unexpanded_type=t) | ||
fix_instance( | ||
res, | ||
self.fail, | ||
self.note, | ||
disallow_any=disallow_any, | ||
use_generic_error=True, | ||
unexpanded_type=t) | ||
return res | ||
elif isinstance(node, TypeInfo): | ||
return self.analyze_type_with_type_info(node, t.args, t) | ||
|
@@ -319,13 +329,14 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt | |
|
||
def get_omitted_any(self, typ: Type, fullname: Optional[str] = None) -> AnyType: | ||
disallow_any = not self.is_typeshed_stub and self.options.disallow_any_generics | ||
return get_omitted_any(disallow_any, self.fail, typ, fullname) | ||
return get_omitted_any(disallow_any, self.fail, self.note, typ, fullname) | ||
|
||
def analyze_type_with_type_info(self, info: TypeInfo, args: List[Type], ctx: Context) -> Type: | ||
"""Bind unbound type when were able to find target TypeInfo. | ||
|
||
This handles simple cases like 'int', 'modname.UserClass[str]', etc. | ||
""" | ||
|
||
if len(args) > 0 and info.fullname() == 'builtins.tuple': | ||
fallback = Instance(info, [AnyType(TypeOfAny.special_form)], ctx.line) | ||
return TupleType(self.anal_array(args), fallback, ctx.line) | ||
|
@@ -337,7 +348,7 @@ def analyze_type_with_type_info(self, info: TypeInfo, args: List[Type], ctx: Con | |
instance = Instance(info, self.anal_array(args), ctx.line, ctx.column) | ||
# Check type argument count. | ||
if len(instance.args) != len(info.type_vars) and not self.defining_alias: | ||
fix_instance(instance, self.fail, | ||
fix_instance(instance, self.fail, self.note, | ||
disallow_any=self.options.disallow_any_generics and | ||
not self.is_typeshed_stub) | ||
|
||
|
@@ -891,10 +902,10 @@ def tuple_type(self, items: List[Type]) -> TupleType: | |
TypeVarList = List[Tuple[str, TypeVarExpr]] | ||
|
||
# Mypyc doesn't support callback protocols yet. | ||
FailCallback = Callable[[str, Context, DefaultNamedArg(Optional[ErrorCode], 'code')], None] | ||
MsgCallback = Callable[[str, Context, DefaultNamedArg(Optional[ErrorCode], 'code')], None] | ||
|
||
|
||
def get_omitted_any(disallow_any: bool, fail: FailCallback, | ||
def get_omitted_any(disallow_any: bool, fail: MsgCallback, note: MsgCallback, | ||
typ: Type, fullname: Optional[str] = None, | ||
unexpanded_type: Optional[Type] = None) -> AnyType: | ||
if disallow_any: | ||
|
@@ -907,17 +918,33 @@ 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) | ||
fail( | ||
message_registry.BARE_GENERIC.format( | ||
quote_type_string(type_str)), | ||
typ, | ||
code=codes.TYPE_ARG) | ||
|
||
if fullname in GENERIC_STUB_NOT_AT_RUNTIME_TYPES: | ||
# Recommend `from __future__ import annotations` or to put type in quotes | ||
# (string literal escaping) for classes not generic at runtime | ||
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 commentThe 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. |
||
"see https://mypy.readthedocs.io/" | ||
"en/latest/common_issues.html#not-generic-runtime", | ||
typ, | ||
code=codes.TYPE_ARG) | ||
|
||
any_type = AnyType(TypeOfAny.from_error, line=typ.line, column=typ.column) | ||
else: | ||
any_type = AnyType(TypeOfAny.from_omitted_generics, line=typ.line, column=typ.column) | ||
return any_type | ||
|
||
|
||
def fix_instance(t: Instance, fail: FailCallback, | ||
def fix_instance(t: Instance, fail: MsgCallback, note: MsgCallback, | ||
disallow_any: bool, use_generic_error: bool = False, | ||
unexpanded_type: Optional[Type] = None) -> None: | ||
unexpanded_type: Optional[Type] = None,) -> None: | ||
"""Fix a malformed instance by replacing all type arguments with Any. | ||
|
||
Also emit a suitable error if this is not due to implicit Any's. | ||
|
@@ -927,7 +954,7 @@ def fix_instance(t: Instance, fail: FailCallback, | |
fullname = None # type: Optional[str] | ||
else: | ||
fullname = t.type.fullname() | ||
any_type = get_omitted_any(disallow_any, fail, t, fullname, unexpanded_type) | ||
any_type = get_omitted_any(disallow_any, fail, note, t, fullname, unexpanded_type) | ||
t.args = [any_type] * len(t.type.type_vars) | ||
return | ||
# Invalid number of type parameters. | ||
|
@@ -950,7 +977,7 @@ def fix_instance(t: Instance, fail: FailCallback, | |
|
||
|
||
def expand_type_alias(target: Type, alias_tvars: List[str], args: List[Type], | ||
fail: FailCallback, no_args: bool, ctx: Context, *, | ||
fail: MsgCallback, no_args: bool, ctx: Context, *, | ||
unexpanded_type: Optional[Type] = None, | ||
disallow_any: bool = False) -> Type: | ||
"""Expand a (generic) type alias target following the rules outlined in TypeAlias docstring. | ||
|
@@ -998,7 +1025,7 @@ def set_any_tvars(tp: Type, vars: List[str], | |
newline: int, newcolumn: int, *, | ||
from_error: bool = False, | ||
disallow_any: bool = False, | ||
fail: Optional[FailCallback] = None, | ||
fail: Optional[MsgCallback] = None, | ||
unexpanded_type: Optional[Type] = None) -> ProperType: | ||
if from_error or disallow_any: | ||
type_of_any = TypeOfAny.from_error | ||
|
@@ -1181,20 +1208,21 @@ def make_optional_type(t: Type) -> Type: | |
return UnionType([t, NoneType()], t.line, t.column) | ||
|
||
|
||
def fix_instance_types(t: Type, fail: FailCallback) -> None: | ||
def fix_instance_types(t: Type, fail: MsgCallback, note: MsgCallback) -> None: | ||
"""Recursively fix all instance types (type argument count) in a given type. | ||
|
||
For example 'Union[Dict, List[str, int]]' will be transformed into | ||
'Union[Dict[Any, Any], List[Any]]' in place. | ||
""" | ||
t.accept(InstanceFixer(fail)) | ||
t.accept(InstanceFixer(fail, note)) | ||
|
||
|
||
class InstanceFixer(TypeTraverserVisitor): | ||
def __init__(self, fail: FailCallback) -> None: | ||
def __init__(self, fail: MsgCallback, note: MsgCallback) -> None: | ||
self.fail = fail | ||
self.note = note | ||
|
||
def visit_instance(self, typ: Instance) -> None: | ||
super().visit_instance(typ) | ||
if len(typ.args) != len(typ.type.type_vars): | ||
fix_instance(typ, self.fail, disallow_any=False, use_generic_error=True) | ||
fix_instance(typ, self.fail, self.note, disallow_any=False, use_generic_error=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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+.