0

When writing a simple python program like the following, pyright can detect that I have the keys 1, 2, 3 in my map, as well as 10, 11 which were added afterwards using the [ ] notation.

Can I achieve the same sort of inference for a custom type, e.g. the following?

from typing import Mapping
class A(Mapping):
    def __init__(self):
        self._map = {1: "a", 2: "b", 3: "c"}

    def __getitem__(self, i: int) -> str:
        return self._map[i]
    def __setitem__(self, i, val) -> None:
        self._map[i] = val
    def __iter__(self):
        return self._map.__iter__()
    def __len__(self):
        return self._map.__len__()

a = A()
a[...  # Can I get autosuggetions for 1, 2, 3 here?
a[10] = 15
a[...  # Can I get autosuggetsions for 1, 2, 3, *10* here?

I understand that pyright will not actually run my code, but I was wondering whether the above was possible or whether the same trick as in the builtin map is exposed for custom classes as well.

bergercookie
  • 2,542
  • 1
  • 30
  • 38
  • 2
    "When writing a simple python program like the following, mypy can detect that I have the keys 1, 2, 3 in my map, as well as 10, 11 which were added afterwards using the [ ] notation." - what makes you say that? – user2357112 Dec 13 '21 at 19:11
  • Actually, no, my bad. I'm using pyright in my editor to provide autocompletion and that's the software I am referring to. Apologies, i'll amend the tag and description – bergercookie Dec 14 '21 at 12:08

0 Answers0