-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot access member "hex" for type "ByteString" #85736
Comments
I get this error when running pyright for a type of typing.ByteString. All the implementations of ByteString (bytes, bytearray, memoryview) have the hex method, so this seems unexpected? |
Without some example code that shows the problem we can't help you. Have you considered that this is a bug with pyright, not a bug with python itself? |
cpython/Lib/_collections_abc.py Line 953 in 48b069a
Line 1612 in 0e95bbf
|
We need to know how to trigger the problem you're seeing. You need to provide code we can run that shows the error you're seeing. |
$ pyright hex.py
stubPath /Users/anacrolix/src/dht-scraper/typings is not a valid directory.
Assuming Python platform Darwin
Searching for source files
Found 1 source file
/Users/anacrolix/src/dht-scraper/hex.py
3:9 - error: Cannot access member "hex" for type "ByteString"
Member "hex" is unknown (reportGeneralTypeIssues)
1 error, 0 warnings
Completed in 0.562sec
anacrolix@anacrolix-mbp-2018:~/src/dht-scraper$ mypy hex.py
hex.py:3: error: "ByteString" has no attribute "hex"
Found 1 error in 1 file (checked 1 source file)
anacrolix@anacrolix-mbp-2018:~/src/dht-scraper$ python3 hex.py
deadbeef |
This looks like a problem in pyright, not in CPython. |
I do not think so. mypy has the same issue. The ByteString type does not include the methods shared by all its implementations. I already linked to this in https://bugs.python.org/msg375553. I also showed that mypy doesn't work in my last comment. |
hex.py works for me with CPython versions 3.5, 3.7, 3.8, and 3.9, and the master branch. |
based on this note in
|
First, note that static type checkers don't actually look at code in CPython, they look at the definition in typeshed: https://github.com/python/typeshed/blob/1d7ace353a4c96b6a279ed0df9179118467da7c7/stdlib/typing.pyi#L708 But it is true that typeshed is just reflecting the code that's in CPython, so decisions should be made here. The interface for cpython/Lib/_collections_abc.py Line 1090 in 1ae7abf
XXX comment that maybe suggests that this wasn't as thought out as it could be.
I don't think this is well specified. As I mention here https://discuss.python.org/t/pep-688-making-the-buffer-protocol-accessible-in-python/15265/16 , if you intersect bytes, bytearray and memoryview you get a Overall, I vote to close this. It's probably problematic from a compatibility standpoint to change interfaces in If you want to type your code loosely, just use If you want to type your code exactly, use a custom protocol, e.g.:
|
Also see the discussion at #91896 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: