Skip to main content

Generate stub file for yacs config.

Project description

yacs-stubgen

Add typing support for your YACS config by generating stub file.

python version workflow

screencap

Install

Install from PyPI:

pip install yacs-stubgen
Other methods

Install from this repo directly:

pip install git+https://github.com/JamzumSum/yacs-stubgen.git

Or you can download from our GitHub release and install package manually.

Usage

Auto-Generate

Add typing support for your yacs config by appending just three lines:

from yacs.config import CfgNode as CN

_C.MODEL.DEVICE = 'cuda'
...
# your config items above

# this line can be moved to the import header
from yacs_stubgen import build_pyi
# this alias ensure you can import `AutoConfig` and use something like `isinstance`
AutoConfig = CN
# _C is the CfgNode object, "_C" should be its varname correctly
# AutoConfig is an alias of CfgNode, "AutoConfig" should be its varname correctly
build_pyi(_C, __file__, cls_name='AutoConfig', var_name='_C')

After any run/import of this file, a stub file (*.pyi) will be generated. Then you will get typing and auto-complete support if your IDE supports stub files.

Each time you change your config, you have to run/import this file again to apply the changes.

Build Script

We have provided a script as an entrypoint. Simply run yacstub <file/dir> and it will generate stub file if one module contains a CfgNode object in global scope.

> yacstub ./conf    # specify a directory
INFO: Generated conf/default.pyi
> yacstub ./conf/default.py # specify a file
INFO: Generated conf/default.pyi

Similarly, each time you change the config, you have to re-run the script to apply the changes.

How it works

Stub files take precedence in the case of both filename.py and filename.pyi exists. Once you pass in the config node, we will iterate over it and generate a stub file then save it as filename.pyi (that's why a path is required). Now supporting IDE will detect the stub file and is able to type-check and intellisense your code.

However, the stub file does nothing with actual code executing. If you import the generated class (default as "AutoConfig"), an ImportError will be raised. This time you can add a variable (aka. type alias) refers to CfgNode in the *.py file. We will override the type of this alias to our generated class ("AutoConfig") in the stub file. Thus you can import the "AutoConfig" normally and intuitively, while the type alias is treated as "AutoConfig" by IDE but is actually a CfgNode type.

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yacs_stubgen-0.3.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yacs_stubgen-0.3.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file yacs_stubgen-0.3.1.tar.gz.

File metadata

  • Download URL: yacs_stubgen-0.3.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yacs_stubgen-0.3.1.tar.gz
Algorithm Hash digest
SHA256 433394db4d90bba42138f51380513d9f55c93c94ce851f4b5bdc78de999d33bd
MD5 1bbcf18c79cbbbb793a594b1dbf499e4
BLAKE2b-256 42f738c67a7a70e05fd9edf17951bac48ca95a060b73e1839aea8c275f84052f

See more details on using hashes here.

File details

Details for the file yacs_stubgen-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: yacs_stubgen-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yacs_stubgen-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba92211f80605e7ba406e5f0db283b28c734c4cff20ec7827620fb01999ccd62
MD5 69dd2114f75eb4ab003eb3b610c6513b
BLAKE2b-256 f7beab94ed0397e84f08b9ae581d6d5e03c46fefaa7ea9a5a8e705ed12a9ec39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page