Skip to content
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

Detect if required string literal escapes are missing from a type #948

Open
JukkaL opened this issue Oct 17, 2015 · 2 comments
Open

Detect if required string literal escapes are missing from a type #948

JukkaL opened this issue Oct 17, 2015 · 2 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 17, 2015

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?

@ethanhs
Copy link
Collaborator

ethanhs commented 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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants