PyCharm type hint warning in `print()` function when passing in a file - python-3.x

The screenshot above is pretty self-explanatory. PyCharm throws a warning, but as far as I understand, the code above is a legitimate way of using the print() function, isn't it?

Related

What is supported signatures and Union[]?

If you wish to see base line questions skip problem details and see summary
Problem Details
I'm not too familiar with gui programming but I'm trying to get the smach_viewer to work for a project I'm working on for my class in ROS noetic. I've resorted to pulling all of the source code and putting it my workspace which already makes it more manageable but while adapting the code in one of the python packages I ran into an error I can't make heads or tails of:
File "/home/hawk/final_project_ws/src/final-project-group-4-inc/src/xdot/xdot_qt.py", line 1914, in main
app.setWindowIcon(QIcon(":/icon.png"))
TypeError: 'PySide6.QtGui.QGuiApplication.setWindowIcon' called with wrong argument types:
PySide6.QtGui.QGuiApplication.setWindowIcon(QIcon)
Supported signatures:
PySide6.QtGui.QGuiApplication.setWindowIcon(Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap])
Above its saying that the function setWindowIcon has a supported signature, which is something I've never seen before. And within the supported signature it says that the parameter for the function looks along the lines of this: Union[QIcon, QPixmap](<-- summarized form). I've never seen Union[] thing before so that is new to me as well.
Summary
What is this error telling me?
What is a Supported Signature?
What is a Union[] supposed to be within parameters as shown in the suggested signature of the error?
So I found out supported signatures specifies what objects you can pass into a parameter.
Union is another way of saying the parameters should be this object or the other.
So the supported signature: PySide6.QtGui.QGuiApplication.setWindowIcon(Union[PySide6.QtGui.QIcon, PySide6.QtGui.QPixmap]) Is saying that function PySide6.QtGui.QGuiApplication.setWindowIcon() accepts an object of type Pyside6.QtGui.QIcon or PySide6.QtGui.QPixmap.

Why does VSCode show 'problems' which do not exist?

I'm using VSCode with the python extension. The 'problems' tab as well as the indicators on the 'explorer' tab and the red underline in the code view itself all show there to be an error, even though the error isn't real.
After importing matplotlib.pyplot as plt, the code uses plt.cm.RdBu. VSCode is presenting the error that matplotlib.cm has no member RdBu.
There are two issues here:
Unless I'm just mistaken, plt.cm.RdBu ought to be equivalent to matplotlib.pyplot.cm.RdBu, not matplotlib.cm.RdBu. It seems to be interpreting that incorrectly.
But regardless of that, both cm and pyplot.cm actually do have an attribute called RdBu, both of which I was able to pull up information for using help(). The code runs perfectly and python throws no errors.
Why is VSCode telling me this is an error when it isn't? Is this just a bug or could it be a misconfiguration?
Unfortunately, there doesn't seem to be a way to fix this issue. The reason that VS Code, or more specifically, the linter used by the Python extension isn't picking up matplotlib.pyplot.cm.RdBu, or any other colormaps defined in cm is because they're defined dynamically. If you try looking inside cm.py, you'll only find this:
cmap_d = _gen_cmap_d()
locals().update(cmap_d)
Essentially, instead of being statically defined like RdBu = ..., these colormaps are defined dynamically by being injected into the module's global namespace at runtime. Even though this works fine when you run it, unfortunately it means that there's no way for the linter to know that names such as RdBu exist in the namespace until runtime. And as most linters such as pylint and flake8 are static code analysis tools that never actually execute the code, it's impossible for them to detect the existence of these colormaps.

How to clearly view the object type?

I'm quite a beginner in Python and have been using PyCharm for some weeks.
I'm trying VS Code at the moment and when I'm putting code into the REPL, I wonder if there's a way to display the different objects type ? ... like Pycharm can do in a side frame ;-)
Thank you
You can always return it with the type function.
type(object)

PTVS2.1 for VS2012 IntelliSense not work

I already refresh DB!
The example can be work.
My problem is IntelliSense is work on line 5, 6
But at the line 7, tree(parameter) can't not find the method xpath()
IntelliSense is not work on line 7, why?
I try to find the answer, someone say need to Removing project __init__.py can fix the problem.
Where is the __init__.py ?
And there exists other good method to solve problem? like: update VS2013?
This is actually just a limitation of PTVS. To figure out the type of tree, it needs to figure out what etree.parse will return when passed a StringIO and HTMLParser. Depending on the code in parse, this may be near impossible to do without actually executing it.
If you hover over tree, I suspect you'll see that it is an unknown type. To force it to have a certain type, you can write:
assert isinstance(tree, WhateverType)
This will let PTVS know that it will definitely be of that type, though at runtime your program will crash if you are wrong. When support for type hints is added, you will be able to use those instead (but that will likely require updating to the very latest version of Visual Studio).

weak warning in PyCharm: Unexpected argument

This inspection reports discrepancies between declared parameters and actual arguments, as well as incorrect arguments (e.g. duplicate named arguments) and incorrect argument order. Decorators are analyzed, too.
^That's what PyCharm is telling me. It's a weak warning, so my code runs fine.
import collections
var_dict = {}
var_dict = collections.OrderedDict(sorted(var_dict.items()))
^This is the line of code in question. I believe the warning has to do with the OrderedDict call.
I checked the OrderedDict documentation for Python 3.5, but I'm still nonplussed.
Why am I getting this warning? I'm using PyCharm Community Edition 5.0.1
I posted an issue - having similar warnings in Python 2. I believe it's a bug in their inspection (in PyCharm 5), but let's see how they respond.
To moderators: this is a valid answer as recognized by the OP. It is not a "comment". Please read carefully before deleting.
So in my case I have a class with a __new__ method which contains the allowed input arguments but I lacked the __init__ method because my class is immutable. My code that has this warning looks like instance = MyClass(a=1, b=2)
Copying over the __new__ method signature into __init__ made this warning go away.

Resources