Running into an issue with a project from here
Right off the bat when testing on Debian running python 3.7 it’s putting out errors, unfortunately I’m no coder and the devs don’t seem too responsive based on previous issues reported on their repos. I think it might be related to the float but what do I know.
def connect_tcp(host="127.0.0.1", port=8423, timeout: float = None) -> tuple[socket.socket, socket.socket]: TypeError: 'type' object is not subscriptable
Any pointers would be greatly appreciated
Not tried much as not sure what do to.
The issue comes from tuple[socket.socket, socket.socket].
This notation using tuple directly was implemented in python 3.9, for python up to 3.8 it was done through typing.Tuple.
You can add from __future__ import annotation (see __future__) at the top of that file to turn on the postponed evaluation of annotations, which will allow you to run with any python from 3.7.
Here is my code:
from collecionts.abc import Sequence
from typing import TypeVar
T = TypeVar('T')
def first(a: Sequence[T]) -> T:
return a[0]
In my understanding, I can pass any Sequence-like object as parameter into first function, like:
first([1,2,3])
and it returns 1
However, it raises a TypeError:' ABCMeta' object is not subscriptable. What is going on here? How can I make it work that I have a function using typing module which can take first element whatever its type?
UPDATE
If I use from typing import Sequence,it runs alright,what is the difference between from collections.abc import Sequence and from typing import Sequence
two things.
The first one is that the typing module will not raise errors at runtime if you pass arguments that do not respect the type you indicated. Typing module helps for general clarity and for intellisense or stuff like that.
Regarding the error that you encounter is probably beacuse of the python version you are using. Try to upgrade to python >= 3.9
system: fedora 31
Pycharm doesn't get me auto completion when type my_var_hash. , there is no problem with venv.
How use Type-Hints with modules like hashlib and that way can we use auto complete options (CTRL+space).
Example of code bellow but auto completion doesn't work.(functions: my_var_hash.update(b'lmao') , my_var_hash.hexdigest() ).
import hashlib
my_var_hash = hashlib.sha256()
print(my_var_hash)
my_var_hash.update(b'lmao')
print(my_var_hash.hexdigest())
print(type(my_var_hash.hexdigest()))
the problem is that hashlib.sha256() does not have type defined or some docstring (when using standard cptyhon)
PyCharm therefore assigns it the "Any" type that does not have any extra info
This is a library issue, not a problem with your setup
This question already has answers here:
How do I type hint a method with the type of the enclosing class?
(7 answers)
Closed 3 years ago.
class Node:
def append_child(self, node: Node):
if node != None:
self.first_child = node
self.child_nodes += [node]
How do I do node: Node? Because when I run it, it says name 'Node' is not defined.
Should I just remove the : Node and instance check it inside the function?
But then how could I access node's properties (which I would expect to be instance of Node class)?
I don't know how implement type casting in Python, BTW.
"self" references in type checking are typically done using strings:
class Node:
def append_child(self, node: 'Node'):
if node != None:
self.first_child = node
self.child_nodes += [node]
This is described in the "Forward references" section of PEP-0484.
Please note that this doesn't do any type-checking or casting. This is a type hint which python (normally) disregards completely1. However, third party tools (e.g. mypy), use type hints to do static analysis on your code and can generate errors before runtime.
Also, starting with python3.7, you can implicitly convert all of your type-hints to strings within a module by using the from __future__ import annotations (and in python4.0, this will be the default).
1The hints are introspectable -- So you could use them to build some kind of runtime checker using decorators or the like if you really wanted to, but python doesn't do this by default.
Python 3.7 and Python 4.03.10 onwards
PEP 563 introduced postponed evaluations, stored in __annotations__ as strings. A user can enable this through the __future__ directive:
from __future__ import annotations
This makes it possible to write:
class C:
a: C
def foo(self, b: C):
...
Starting in Python 3.10 (release planned 2021-10-04), this behaviour will be default.
Edit 2020-11-15: Originally it was announced to be mandatory starting in Python 4.0, but now it appears this will be default already in Python 3.10, which is expected 2021-10-04. This surprises me as it appears to be a violation of the promise in __future__ that this backward compatibility would not be broken until Python 4.0. Maybe the developers consider than 3.10 is 4.0, or maybe they have changed their mind. See also Why did __future__ MandatoryRelease for annotations change between 3.7 and 3.8?.
In Python > 3.7 you can use dataclass. You can also annotate dataclass.
In this particular example Node references itself and if you run it you will get
NameError: name 'Node' is not defined
To overcome this error you have to include:
from __future__ import annotations
It must be the first line in a module. In Python 4.0 and above you don't have to include annotations
from __future__ import annotations
from dataclasses import dataclass
#dataclass
class Node:
value: int
left: Node
right: Node
#property
def is_leaf(self) -> bool:
"""Check if node is a leaf"""
return not self.left and not self.right
Example:
node5 = Node(5, None, None)
node25 = Node(25, None, None)
node40 = Node(40, None, None)
node10 = Node(10, None, None)
# balanced tree
node30 = Node(30, node25, node40)
root = Node(20, node10, node30)
# unbalanced tree
node30 = Node(30, node5, node40)
root = Node(20, node10, node30)
If you just want an answer to the question, go read mgilson's answer.
mgilson's answer provides a good explanation of how you should work around this limitation of Python. But I think it's also important to have a good understanding of why this doesn't work, so I'm going to provide that explanation.
Python is a little different from other languages. In Python, there's really no such thing as a "declaration." As far as Python is concerned, code is just code. When you import a module, Python creates a new namespace (a place where global variables can live), and then executes each line of the module from top to bottom. def foo(args): code is just a compound statement that bundles a bunch of source code together into a function and binds that function to the name foo. Similarly, class Bar(bases): code creates a class, executes all of the code immediately (inside a separate namespace which holds any class-level variables that might be created by the code, particularly including methods created with def), and then binds that class to the name Bar. It has to execute the code immediately, because all of the methods need to be created immediately. Because the code gets executed before the name has been bound, you can't refer to the class at the top level of the code. It's perfectly fine to refer to the class inside of a method, however, because that code doesn't run until the method gets called.
(You might be wondering why we can't just bind the name first and then execute the code. It turns out that, because of the way Python implements classes, you have to know which methods exist up front, before you can even create the class object. It would be possible to create an empty class and then bind all of the methods to it one at a time with attribute assignment (and indeed, you can manually do this, by writing class Bar: pass and then doing def method1():...; Bar.method1 = method1 and so on), but this would result in a more complicated implementation, and be a little harder to conceptualize, so Python does not do this.)
To summarize in code:
class C:
C # NameError: C doesn't exist yet.
def method(self):
return C # This is fine. By the time the method gets called, C will exist.
C # This is fine; the class has been created by the time we hit this line.
I have problems with Python 3.2 and PyQt 4.8.6
It seems as if Python 3.2 can`t find the imports.
Especially the "Q"-methods. For example the QString below.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
With Python 2.7 everything works fine.
Where is the mistake in my code?
Python3 made many incompatible changes in order to "clean up" the language, and, to a certain extent, PyQt has done the same by introducing "more pythonic" versions of some APIs. But these different API versions can be selected on a class by class basis for both Python2 and Python3, so the only real difference is the defaults chosen for each Python version.
In Python2, the default API version for QString is "v1", which implements it as a Python type; in Python3 the default is "v2", which automatically converts to and from the appropriate Python string object.
The API version can be selected by using the setapi function from the sip package. So to continue using the QString class in your application, just make sure the appropropriate version is set before the PyQt modules are first imported:
import sip
sip.setapi('QString', 1)
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
For details of all the APIs that can be set this way, see here.
Take a look at the notes about Python 3 in the PyQt Reference Guide.
The QString class is implemented as a mapped type that is
automatically converted to and from a Python string. In addition a
None is converted to a null QString. However, a null QString is
converted to an empty Python string (and not None). (This is because
Qt often returns a null QString when it should probably return an
empty QString.)
I've not moved any code over to Python 3 yet, but I believe that the idea is to use normal Python strings instead of QStrings. PyQt will accept them and they already support unicode in Python 3. Where normally a PyQt function would return a QString it will return a regular python string under Python 3.
Have a look at the other differences on the linked page too.