Detect if required string literal escapes are missing from a type

Open
#948 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start by tracing mypy's semantic analysis for forward references and the handling of quoted versus unquoted type names. Check how class definitions and cyclic imports are represented, and account for the issue's note about type aliases. Done means reporting missing required string-literal escapes, initially within a single module and eventually across cyclic imports.

Written by the indexing model from the issue text.

Description

feature priority-1-normal topic-quoted-annotations topic-usability

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?

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.