Search code examples
pythonpycharmpython-typing

Python - curly braces in type hints


What do these mean?

 def f(a: {int, float}):
    pass

I've seen this syntax used in some standard Python modules when fetching documentation via PyCharm, and I have no idea what it means. What's the hinted type for a in my example? What types can I pass to this function?

The particular example where I've seen this is in tkinter's Frame __init__ method, where the master parameter is of type {tk, _w}.


Solution

  • It's a hint telling you it wants an object with the named attributes 'int' and 'float' -- or more specifically for tkinter 'tk' and '_w'

    I coded up a minimal example in pycharm:

    enter image description here

    Inpecting the python library sources -- You can see that there are attempted accesses to master.tk and master._w. That's all that pycharm was able to infer about the type of the parameter master so it floated it up to the IDE in this manner.