Skip to content

Fix type inference of subscripted list slice#7689

Open
matusvalo wants to merge 3 commits into
cython:masterfrom
matusvalo:subscripted_list_slice
Open

Fix type inference of subscripted list slice#7689
matusvalo wants to merge 3 commits into
cython:masterfrom
matusvalo:subscripted_list_slice

Conversation

@matusvalo
Copy link
Copy Markdown
Contributor

@matusvalo matusvalo commented May 13, 2026

Fixes #7688

@matusvalo matusvalo added this to the 3.3 milestone May 13, 2026
@matusvalo matusvalo changed the title Fix type inferation of subscripted list slice Fix type inference of subscripted list slice May 13, 2026
@matusvalo
Copy link
Copy Markdown
Contributor Author

matusvalo commented May 15, 2026

I am taking liberty and merge this change without code review because:

  • it fixes nasty bug which causes CI fail in other PRs
  • it affects only subscripted types
  • change is simple and limited

If there are any comments/improvements can be implemented in another PR...

@matusvalo matusvalo enabled auto-merge (squash) May 15, 2026 20:53
@matusvalo matusvalo disabled auto-merge May 16, 2026 05:31
@da-woods
Copy link
Copy Markdown
Contributor

I don't think any of the test failures are particularly relevant here (they all affect every other branch right now and I think aren't issues that we can fix). I don't fully understand ones on the Python development branch; I've reported the pypu one to pypy.

@matusvalo
Copy link
Copy Markdown
Contributor Author

Yeah I ser. Unfortunately it blocks my automerge ...

@da-woods
Copy link
Copy Markdown
Contributor

One extra check I'd add here:

d = { slice(1, 5): "some_value" }
cython.typeof(d[1:5])

In this case the return type should be a single element (or just object).

I've never seen this used in real code so I think the main thing is that it doesn't infer something incorrect like a dict return type. But it's fine if it just says "object"

@matusvalo
Copy link
Copy Markdown
Contributor Author

One extra check I'd add here:

d = { slice(1, 5): "some_value" }
cython.typeof(d[1:5])

In this case the return type should be a single element (or just object).

I've never seen this used in real code so I think the main thing is that it doesn't infer something incorrect like a dict return type. But it's fine if it just says "object"

I think already cython in master contains error your code produces dict object. Python on the other hand:

>>> d = { slice(1, 5): "some_value" }
>>> d[1:5]
'some_value'

@scoder
Copy link
Copy Markdown
Contributor

scoder commented May 18, 2026

I don't fully understand ones on the Python development branch

See python/cpython#149583 (comment)

@scoder
Copy link
Copy Markdown
Contributor

scoder commented May 18, 2026

d = { slice(1, 5): "some_value" }
cython.typeof(d[1:5])

We could add the following to the start of the analyse_as_pyobject method:

if is_slice and base_type.is_pyanydict_type:
    is_slice = False

For a dict, a slice lookup is never special.

@scoder scoder added the defect label May 18, 2026
@matusvalo
Copy link
Copy Markdown
Contributor Author

d = { slice(1, 5): "some_value" }
cython.typeof(d[1:5])

We could add the following to the start of the analyse_as_pyobject method:

if is_slice and base_type.is_pyanydict_type:
    is_slice = False

For a dict, a slice lookup is never special.

The fix is totally unrelated to this PR so I fixed it in separate PR: #7702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Type error for range index of subscripted list

3 participants