Skip to content

Alternate approach to fix list.pop() return type inference - #47

Merged
Tobias-Kohn merged 1 commit into
Tobias-Kohn:dev-tkfrom
neilccbrown:list-pop-item-type-dev-tk
Aug 6, 2026
Merged

Tobias-Kohn merged 1 commit into
Tobias-Kohn:dev-tkfrom
neilccbrown:list-pop-item-type-dev-tk

Conversation

@neilccbrown

Copy link
Copy Markdown
Contributor

This is an alternate approach to pull request #45 to achieve the same aim, based on your dev-tk branch. (Note the PR is targeting to merge into that branch, not into master.)

Reminder of the problem: pop() was hard-coded to return the list type itself instead of the item type, so e.g. get_actors().pop().remove() resolved .remove() against list.remove(x) instead of the actual item class's remove(), leaking the wrong signature into autocomplete. Also append/extend/insert/remove/ reverse/sort would return a list type, rather than None.

This PR mirrors the BuiltinMethod/getMethodType pattern used for dict.keys() rather than an AST-walker echo marker: MUTABLE_SEQ registers "pop" as a shared BuiltinMethod template, and TypeAstWalker.getTypeOfAttr binds it to the receiver's own PrimitiveType (e.g. list[Actor] rather than plain list) at each attribute access, since a field shared across every instance of a type can't otherwise see which concrete instance it was accessed on. ListType overrides getMethodType to answer "pop" with its item type.

See how you find this alternative.

pop() was hard-coded to return the list type itself instead of the item
type, so e.g. get_actors().pop().remove() resolved .remove() against
list.remove(x) instead of the actual item class's remove(), leaking the
wrong signature into autocomplete. Also fixed append/extend/insert/remove/
reverse/sort, which had the same hard-coded-to-self bug but should return
None.

Mirrors the BuiltinMethod/getMethodType pattern used for dict.keys() rather
than an AST-walker echo marker: MUTABLE_SEQ registers "pop" as a shared
BuiltinMethod template, and TypeAstWalker.getTypeOfAttr binds it to the
receiver's own PrimitiveType (e.g. list[Actor] rather than plain list) at
each attribute access, since a field shared across every instance of a type
can't otherwise see which concrete instance it was accessed on. ListType
overrides getMethodType to answer "pop" with its item type.
@Tobias-Kohn
Tobias-Kohn merged commit 7612555 into Tobias-Kohn:dev-tk Aug 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants