-
Notifications
You must be signed in to change notification settings - Fork 3.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
[python-package] fix type hints on ctypes array converters #5446
Conversation
I think we can instead use |
@jmoralez hmmm, that doesn't look right to me. docker run \
--rm \
--entrypoint="" \
-it python:3.8-slim \
python -c "import ctypes; print(ctypes.POINTER[ctypes.c_int32])"
I think that maybe the comment you found was someone getting lucky (or, I guess, unlucky) with the official type hints for See this from last month: python/typeshed#8351 (comment) |
Ah I see, in the stub the lowercase pointer is a class but in ctypes it isn't haha. I think it's ok to go with Any. |
Should we switch to |
@StrikerRUS I'm nervous about relying on something preceded with a I want LightGBM's Python code to get the benefits of type-hinting, but I'm worried about the risk of user code being broken because of a change in |
Just for reference:
However, I'm OK with |
Thanks for pointing out that comment. Even with that, I'm just nervous about the amount of change happening around this topic (indicated by the issues, you, @jmoralez , and I have linked to) and don't think the benefit of more accurate hints for these small utility functions is worth taking on the risk of incompatibilities of |
This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
Contributes to #3756.
Contributes to #3867.
mypy
currently raises the following errors.These are correctly noting that
ctypes.POINTER
isn't a type...it's a function that returns a type. From https://docs.python.org/3/library/ctypes.html#ctypes.pointerYou can see this with the following example code.
Since the functions
mypy
is complaining about all take in an object, check for a specific type withisinstance()
, and raises an exception if the passed-in object doesn't have that type, I think it's appropriate to replace thosectypes.POINTER
type hints with justAny
.Notes for Reviewers
I tested this by running
mypy
as documented in #3867.mypy \ --exclude='python-package/compile/|python-package/build' \ --ignore-missing-imports \ python-package/