2

Currently I am dealing with a lot of code inspection in pycharm. Often I have the problem that I don't know which is the actual data type of a variable in python. Then I have to revisit the code to find the actual definition of the variable, or use some inserted code snippets like type() to get the data format.

What would be the most elegant / most comfortable way to do this task? I though maybe there are some built-in functions in PyCharm.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
TheGaertner
  • 41
  • 1
  • 2
  • 1
    see the discussion in https://stackoverflow.com/questions/2225038/determine-the-type-of-an-object?rq=1 – Zachi Shtain May 28 '19 at 08:38
  • @ZachiShtain the OP explicitely mentions that he knows about using ` type(obj)` - the question is about how to get those informations in PyCharm without having to insert tracing code everywhere... – bruno desthuilliers May 28 '19 at 08:41
  • Given python's dynamic typing, there's no way to do static type analysis - you only know the exact type of an object at runtime. The only reliable way to know what type a given object is is to inspect it at runtime - which is usually done using a step debugger (either python's builtin `pdb` or PyCharm's own debugger). Note that you'll have to check the various code path that can lead to the portion of the code you want to inspect though. – bruno desthuilliers May 28 '19 at 08:58

2 Answers2

2

Welcome to stackoverflow. You can always try to have a look at the Quick documentation to get a documentation hint in the Jetbrains IDEs.

But since Python is dynamically typed this might not help you every time if you are not using Python type hints and the code is getting "complicated". There is a shortcut to look at the definition of a symbol/variable which will allow you to remember what type you intented to use.

You have to be aware though that you can change what you bind to a variable, meaning that at the beginning foo binds an integer (foo = 0) and later you change it to a string (foo = ""). This is totally fine is Python and therefore the code inspection of PyCharm might give you wrong information.

Björn Böing
  • 1,662
  • 15
  • 23
  • 1
    "the type of a variable can change over time" => this is misleading. What changes is the object the name is bound too, not the type of the object itself. – bruno desthuilliers May 28 '19 at 08:51
  • 1
    "Python is not strictly typed" => python is dynamically typed - the type information belongs to the object, not to the name. But it is "strictly typed" in the sense that a given object will not be interpreted differently by the runtime depending on the context (for example trying to add a string and a number will not turn the string into a number nor the number into a string - you'll get a TypeError instead). – bruno desthuilliers May 28 '19 at 08:55
  • 1
    Thanks, for the clearifications. I edited my answer accordingly. – Björn Böing May 28 '19 at 08:57
  • You may want to read Ned Batchelder's paper on python's names and binding for a comprehensive explanations of python's "variables" https://nedbatchelder.com/text/names.html (this thing should really be part of the official documentation). – bruno desthuilliers May 28 '19 at 09:00
1
  1. Press and hold 'ctrl' for Windows ('command' for macOS)
  2. Move a mouse cursor over the variable of interest

enter image description here

You can see "Inferred type". Sometimes, as in the case above, it is not what the real variable ever be in this place, but at least PyCharm tried it best to infer.

import random
variable = 1
variable = None
for i in range(10):
    variable = random.randint(1, 3)
    if variable <= 0:
        variable = 'Zero or less'
    elif variable == 1:
        variable = 1.0
    elif variable == 2:
        variable = [2]
    elif variable == 3:
        variable = {'three': 3}
    else:
        variable = (4, '4 or more')
    print(i, type(variable), variable)
# Extra blank lines to show variable type
#
#
#
del variable
Alex Lopatin
  • 682
  • 4
  • 8
  • Moving a cursor over the variable of interest with Command hold doesn't show the type to me :-( – AndrewS Mar 11 '21 at 09:25
  • 1
    Yeah, this used to work for me, but no longer does, as of at least a few months ago. Now, it only shows `variable "xyz"` – theY4Kman Apr 20 '21 at 17:19