Skip to content
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

Bytes and bytearray stubs missing ctypes constructor overloads #477

Closed
ELFoglalt opened this issue Oct 12, 2020 · 2 comments
Closed

Bytes and bytearray stubs missing ctypes constructor overloads #477

ELFoglalt opened this issue Oct 12, 2020 · 2 comments

Comments

@ELFoglalt
Copy link

Environment data

  • Language Server version: v2020.10.0
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.0

Expected behaviour

ctypes objects can be used to instantiate bytes and bytearray objects via copying their buffer. The following code should not produce any static analysis errors:

from ctypes import Structure, c_uint8, c_uint16

class A(Structure):
    _pack_ = 1
    _fields_ = [("foo", c_uint8), ("bar", c_uint16)]

print(bytearray(c_uint8(65)))  # bytearray(b'A')
print(bytes(A(65, 17218)))  # b'ABC'

Actual behaviour

The code above runs just fine, but produces the following errors in the editor:

print(bytearray(c_uint8(65)))
#     ~~~~~~~~~~~~~~~~~~~~~~
#     ^ No overloads for "bytearray(c_uint8(65))" match parameters
#       Argument types: (c_uint8)

print(bytes(A(65, 17218)))
#     ~~~~~~~~~~~~~~~~~~~~
#     ^ No overloads for "bytes(A(65, 17218))" match parameters
#       Argument types: (A)

Code Snippet / Additional information

Possibly related: python/typing issue and python bug report. I don't fully expect this to be resolvable in a general sense, until the issues mentioned are resolved, but it would sure be nice to have constructor overloads in buffer-like objects for ctypes objects.

@jakebailey
Copy link
Member

jakebailey commented Oct 12, 2020

Like other type checkers, Pylance/pyright use the stubs from typeshed, which it appears that those linked threads are covering changes to (or some other related typing change). There's not much we can do when these types are defined to have a specific signature for their constructors.

For the specific lines if you know the invocations to be correct, you should be able to add # type: ignore to tell the type checker to suppress any messages, though.

@ELFoglalt
Copy link
Author

My bad, this is clearly to be solved in the dependency then. I wrongfully attributed the .pyi definitions in question to be pylance related. I'll just use the ignore hint to get rid of the errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants