python / python/mypy

Disallow assignment of `Any` to known/explicit types and/or Introduce `Unknown` type

Open
#10,173 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature

I'd like an option that prevents assigned of the Any type to something with a known type, in particular places where I've specified the type.

Returns:

def return_type_a() -> TypeA:
   untyped = {}
   return untype  # Disallow

Assignement:

store : Dict[StoreRef,StoreItem = {}

def create() -> Dict:
  return {}

store = create()  # Disallow

Pitch

It'd be nice to have a stricter boundary between typed and untyped code. The samples I show above are frequent sources of error for me and they quietly slip by the type checking.

While migrated code there are still a lot of Any's in the code, and in some domains they'll never go away (when dealing with genuinely generic data structures, also now since some recrusive types, like JSON, need to use Any).

Related

An explicit Unknown type, in contrast to Any, would also help a lot. TypeScript has an unknown and it is helpful for dealing with generic data and not accidentally using it as the wrong type.

If an Unknown was available the above could also be achieved by turning on the "disallow any" flags and using Unknown in places where generic data is needed.

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 reading mypy's handling of Any and its existing disallow-any flags, using the return and assignment examples as behavioral cases. Clarify whether the work covers stricter assignment checks, an Unknown type, or both; done means the selected behavior is specified and enforced without breaking legitimate generic-data use.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.