python / python/mypy

No error when attempting to use property object as that property's type

Open
#6,192 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-2-low topic-descriptors topic-named-tuple topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

  • Are you reporting a bug, or opening a feature request? Bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import NamedTuple


class C(NamedTuple):
    a: int = 1


print(C.a * 2)
  • What is the actual behavior/output?

mypy a.py produces no output.

  • What is the behavior/output you expect?

An error at C.a * 2, as it will cause an error at runtime: TypeError: unsupported operand type(s) for *: 'property' and 'int'

  • What are the versions of mypy and Python you are using?

python --version: 3.7.2
mypy --version: 0.660+dev.97d7760dca6d95806a97b02992ae88a082898a0e

Do you see the same issue after installing mypy from Git master? Yes

  • What are the mypy flags you are using? (For example --strict-optional) None

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 running mypy a.py with the reported example and confirm that it produces no output while Python raises the stated TypeError. Trace how mypy handles the NamedTuple property access and arithmetic expression; done means mypy reports an error at C.a * 2 for this case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.