Skip to content

NewType/TypeVar/ParamSpec reference and rename

Python typing helpers often repeat a symbol name inside a string literal. BetterPy connects those string literals to the Python symbol so navigation and rename stay consistent.

How to invoke

  • Use ⌘+Click / Ctrl+Click on the name string in NewType, TypeVar, or ParamSpec calls to jump to the matching assigned symbol, for example from "UserId" to UserId.
  • Use ⇧F6 / Shift+F6 on the Python symbol to rename it. BetterPy keeps the matching string literal in sync.
  • For NewType, if the string was edited manually and no longer matches the assigned symbol, use the quick-fix on the highlighted string to update it.

Examples

from typing import NewType

UserId = NewType("UserId", int)
from typing import TypeVar

ItemT = TypeVar("ItemT")
from typing import ParamSpec

P = ParamSpec("P")

Renaming UserId to AccountId updates both the target name and the string literal:

AccountId = NewType("AccountId", int)

If a NewType literal drifted manually, BetterPy highlights the mismatch and offers to update the string:

AccountId = NewType("UserId", int)

The quick-fix rewrites it to:

AccountId = NewType("AccountId", int)

What it supports

  • typing.NewType, typing.TypeVar, and typing.ParamSpec style declarations.
  • String-literal references from the helper call back to the assigned Python symbol.
  • Rename synchronization for the declaration and the literal.
  • A quick-fix for manual NewType string drift when the literal no longer matches the assigned symbol. PyCharm's built-in quick-fix handles the same drift for TypeVar and ParamSpec.
  • Navigation and reference search in regular project files.

What it deliberately avoids

  • Calls where the string literal intentionally does not match the assigned symbol.
  • Dynamic names or computed string arguments.
  • Unrelated string literals that happen to contain the same text.
  • Library and stub files outside your project.

Notes

  • This feature reduces rename drift in typing-heavy modules without changing runtime behavior.
  • The feature is gated by the NewType/TypeVar/ParamSpec reference & rename setting under Settings | BetterPy | Typing & Type Hints | Completions.