python / python/mypy

Feature request: option that completely disallows typing.cast

Open
#17,077 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-casts
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

An option that disallows use of typing.cast, eg --disallow-casts

Pitch

cast() gets around static type safety without ensuring dynamic type correctness. Sometimes you have to use it—you know how it is—but it's not ideal. (For instance, mypy documentation seems to imply the main use of cast is when the typechecker or type annotations aren't quite good enough.) That means, in my opinion, cast is a great candidate to have a option disallowing its use, to ensure code quality in codebases where it is not needed.

I also think this option should be enabled in strict mode, but I suspect other people might disagree.

This proposal is distinct from the --warn-redundant-casts option that already exists, but is somewhat similar.

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.

Research direction

Start by reviewing how the existing --warn-redundant-casts option is implemented and read the discussion about whether this belongs in strict mode. Define the intended handling of typing.cast and its configuration behavior, then verify that the new option rejects casts and that the chosen strict-mode behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.