kRPC: Control the game using C#, C++, Java, Lua, Python...

kRPC: Control the game using C#, C++, Java, Lua, Python...

7.8k Downloads

[Python] Accessing subtypes (eg. Vessel) isn't ergonomic

object-Object opened this issue ยท 7 comments

commented

Many kRPC types can't be directly imported. For example, the Vessel type is only accessible like so:

from krpc.spacecenter import SpaceCenter

vessel: SpaceCenter.Vessel = ...

It would be convenient if, for example, the spacecenter.pyi stub could export those types directly. As an example, in my personal kRPC workspace, I've added additional (non-comprehensive) files to make these types easier to access.

(I'm unsure if this is an issue with base kRPC or just nullprofile's fork, though I suspect the former.)

commented

clientgen is the part responsible for generating the stubs. Source is in tools/krpctools/krpctools/clientgen/python.py
Should be a straightfoward addition.

commented

Oops, I probably should have tried to run that code before submitting this issue. It doesn't seem to be possible to import krpc.spacecenter at all:

(venv) PS C:\Users\jack\Git\kRPC-or-Bust\src> python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import krpc
>>> from krpc import spacecenter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'spacecenter' from 'krpc' (C:\Users\jack\Git\kRPC-or-Bust\venv\Lib\site-packages\krpc\__init__.py)
>>> from krpc.spacecenter import SpaceCenter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'krpc.spacecenter'

So is there any way to get those types for type hinting code?

(Using v1.0-beta-1 from nullprofile's fork)

commented

Okay so it is possible, you just have to do ugly boilerplate like this: python/typing#928

(Here's my implementation, seems to work alright)

It'd be nice if this weren't necessary, though.

commented

What exactly are you trying to achieve? Do you just want to be able to import the types to use them in type hints?

As an example, do you just want to be able to write something like the following:

import krpc
from krpc.spacecenter import Vessel

conn = krpc.connect()
vessel: Vessel = conn.space_center.vessels[0]
commented

Yes, exactly that.

commented

Ok cool. I've almost finished working on a rewrite of the python library, with type hints for everything, not just an ad-hoc (and very buggy) .pyi file on the side. Just about got it passing static type checking with mypy in strict mode.

With this new version explicit type hints like this would be unnecessary, as tools like pycharm/mypy can statically know the type of conn.space_center.vessels[0] is Vessel.

The rewrite does allow you to write it explicitly like this if you want though.

commented

This has been implemented and will be included in v0.5.2.

The syntax for importing types is, for example:

import krpc
from krpc.services.spacecenter import Vessel

conn = krpc.connect()
vessel: Vessel = conn.space_center.vessels[0]

Let me know if you want to try it out and I can send you a pre-release build.