python / python/mypy

Copy with slicing of a list[subclass] should be ok for list[superclass]

Open
#7,456 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive needs discussion priority-2-low topic-usability
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?
    As I expected this to work, I assume it is a 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 List

class Upi(object):
    pass


class Subi(Upi):
    pass


subs = [Subi(), Subi()]
ok1:List[Upi] = [s for s in subs]
ok2:List[Upi] = list(subs)
notOk:List[Upi] = subs[:] # this should also be ok IMHO
  • What is the actual behavior/output?
    notOk list is not ok for mypy.

  • What is the behavior/output you expect?
    Should be ok. Actually saw this in a function return value so should be ok there also when function is returning list of superclass things.

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

  • 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

The issue names no implementation files or tests. Begin by running the supplied repro with mypy, then trace how list slicing is typed compared with a comprehension and list(subs). Done means assigning subs[:] to List[Upi] is accepted without flags, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.