-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
featurepriority-1-normaltopic-quoted-annotationsDetecting problems with quoted annotationsDetecting problems with quoted annotationstopic-usability
Description
Mypy should complain about code that isn't using string literal escaped when they are required:
class A:
def (self) -> A: ... # Should be an error; string literal escape required
Initially we could only detect these cases within a single module. A more general solution would also detect string literal types needed due to cyclic imports.
A simple implementation idea:
- Tag each instance type with information on whether the type is quoted or not.
- During semantic analysis, if a type is unquoted but the definition line for the type/class is in the current file but at a later line number, complain.
- The definition line for a class is actually the line after the end of the class definition. Alternatively, treat classes that are currently being analyzed specially.
We also need deal with type aliases, but this isn't needed for the first iteration.
I think that somebody started working on this a while back, but I can't remember the details. Maybe there is an existing issue as well?
Metadata
Metadata
Assignees
Labels
featurepriority-1-normaltopic-quoted-annotationsDetecting problems with quoted annotationsDetecting problems with quoted annotationstopic-usability
Projects
Milestone
Relationships
Development
Select code repository
Activity
emmatyping commentedon Apr 5, 2018
PEP 563 is also relevant to this, making this only required when the annotations future import hasn't been made (at least for 3.7+).
hauntsaninja commentedon Nov 14, 2020
Note that #9647 does this for use of PEP 604 syntax; it sets an "is_evaluated" attribute on UnionTypes that is False for type comments and string literal types.