1

I am trying to figure out what instance variables do not show up in auto completion in an IPython or Jupyter notebook. For example, if I have the class below:

class A:
    
    def __init__(self, var_a: int):
        self.var_a = var_a

and I define a class B that takes an instance of A at initialization, it seems like autocompletion does not look through to the definition of class A to see that it has a var_a instance attribute.

class B:
    
    def __init__(self, var_b: A):
        self.var_b = var_b
    
    def func(self):
        self.var_b.

I do not get any type hints if I press tab after the last period in the snippet above. Pylance (through the VS Code Python extension) and PyCharm both show autocompletion for self.var_b.var_a. Is there a way to configure either IPython or the code annotations so that autocompletion will also work in IPython?

Using IPython 8.4.0

Tried %config Completer.use_jedi = True in the first notebook cell which did not help.

joudan
  • 81
  • 5

1 Answers1

0

Explicitly annotating the namespaces of the classes should work:

class A:
    var_a: int

    def __init__(self, var_a: int) -> None:
        self.var_a = var_a


class B:
    var_b: A

    def __init__(self, var_b: A) -> None:
        self.var_b = var_b

It doesn't get more explicit than that. If this code doesn't lead to proper type inference/auto-suggestions, that should be considered a bug.

Daniil Fajnberg
  • 12,753
  • 2
  • 10
  • 41
  • Believe the `var_a: int` and `var_b: A` at the top of the class definitions just add annotations. Not sure whether the autocompleters look there. I've [read](https://stackoverflow.com/questions/13603088/python-dynamic-help-and-autocomplete-generation/13603392#13603392) that they look at the attributes returned from `dir`. After further exploration, it seems that defining the classes in separate cells or IPython entries causes the issue. If you define both classes before hitting enter the autocompletion works as anticipated. – joudan Dec 06 '22 at 03:18