Skip to content

Incorrect type inference after __setitem__ / __getitem__ #3299

@MarcoGorelli

Description

@MarcoGorelli

Describe the Bug

from __future__ import annotations

from typing import Any, reveal_type, Protocol

class Series(Protocol):
    def astype(self, dtype: Any) -> Series:
        ...

class DataFrame(Protocol):
    def __setitem__(self, key: str, value: object) -> None:
        ...

    def __getitem__(self, key: str) -> Series:
        ...

def main(df: DataFrame) -> None:
    df['a'] = '!'
    df['a'] = df['a'].astype('string[pyarrow')
(scratch) mgorelli@marcoslaptop:~/scratch$ mypy t.py
Success: no issues found in 1 source file
(scratch) mgorelli@marcoslaptop:~/scratch$ ty check t.py
All checks passed!
(scratch) mgorelli@marcoslaptop:~/scratch$ pyright t.py
0 errors, 0 warnings, 0 informations
(scratch) mgorelli@marcoslaptop:~/scratch$ zuban check t.py
Success: no issues found in 1 source file
(scratch) mgorelli@marcoslaptop:~/scratch$ pyrefly check t.py
ERROR Object of class `str` has no attribute `astype` [missing-attribute]
  --> t.py:18:15
   |
18 |     df['a'] = df['a'].astype('string[pyarrow')
   |               ^^^^^^^^^^^^^^
   |
 INFO 1 error

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEA%2Bg2AK4AuLlMTdENxuSmzoZ0uNqjYRc6OAB10CsNXptSxCOgDmvfoOEBBdKQA0dLgDcYqKAzXEYZgArU2uAMa4oChe6io4ODoAZRhKCHgAChdxDy8ASkQFOhS6TBgwETh7GEi4GCgwM0wcxDojUni6AFoAPhCwiLgk9FS2ukJOn3Q-AKCAEUlUADFKVBpcmLdPKETk1PTMpnypNhgaJjyCoroAaxhSMuzKMwsbFhgy3GwAKxh3Niq6ugA5GUv59o6uxVaFjMYDC0MFW602%2BUKZn2hzoxye9VC4XgLS%2BKU6hG6izoNFQmkimDAZUGElG4xg8Ne7xRCzAAG0AOSoekAXToAF46PSAIT0z5pOmMlns-kMpnMwgBHKRenHTRaWlkVCUagAd3p8QUIBMIDIXDAUFIhDcNCgFAAxHQnKQ9QbYRgcAQ6J50JAtJxJNJ0Bj0BbQjA6AALNhsYjNAD0Yd1GQNhEEWjDMHQYcwHjgYedrvdUhkYboYEEIjO0FQ2FgTpkmbG2dauGI1bg3rIbADMmqVkocE9wrkIAAzIQAIwAJh7ClpYWoHeZChY6D4AiEMEw1UwEC4DwgVmF9IgWjEXF56DEbGqXAAjiw10vqtDqqh3O54EEOfSVUr0IeQABfbX3qRWYZoBgCg0CwPAiDIb8gA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions