python / python/mypy

Clarification of `properly subclassable class` in NewType

Open
#6,701 6 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug documentation false-positive priority-1-normal topic-newtype
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I want to create a new type derived from ObjectId in similar way as is described in documentation for new type UserId derived from int:

from bson import ObjectId
UserId = NewType('UserId', ObjectId)

But Mypy gives Argument 2 to NewType(...) must be subclassable (got "Any").

There is written in documentation:

NewType accepts exactly two arguments. The first argument must be a string literal containing the name of the new type and must equal the name of the variable to which the new type is assigned. The second argument must be a properly subclassable class, i.e., not a type construct like Union, etc.

It is not clear what properly subclassable class means and why ObjectId is not properly subclassable class. Could please someone clarify this to me and in documentation?

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 with the linked NewType documentation section and reproduce the diagnostic using ObjectId. Clarify what “properly subclassable class” means and explain why ObjectId is reported as Any, then verify that the revised wording addresses the example and preserves the existing NewType guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.