Parse Python classes and attributes from a string of Python code.
Project description
PyClassParser
PyClassParser is a Python library designed to parse Python class definitions and extract class attributes. Parsing is backed by the standard-library ast module, so it is robust against blank lines, comments, docstrings, methods, nested classes and default values that contain colons. It uses the attrs library for defining the result objects and ensures type safety with attrs-strict.
Features
- Parses Python class definitions to extract class names and their attributes.
- Uses the standard-library
astmodule, so the input must be syntactically valid Python (aSyntaxErroris raised otherwise). - Uses
attrsfor the result objects andattrs-strictfor type validation. - Captures each attribute's name (
attr_value), annotation (attr_type) and default value (default_value) when present. - Rebuilds a clean, always-valid reconstruction of the imports and class stubs.
Installation with UV:
uv add pyclassparser
Usage
Here's how you can use PyClassParser to parse Python class definitions:
Example Code
Create a Python script, for example example.py:
from pyclassparser import ClassParser
code = """
class MyClass:
attr1: int
attr2: str = "default"
class AnotherClass:
attr3: float
"""
parser = ClassParser(code=code)
print(parser.output)
print(parser.class_data_dict)
Example Output
Running the above script should give you the parsed output of class definitions:
class MyClass:
attr1: int
attr2: str = 'default'
class AnotherClass:
attr3: float
{'MyClass': [ClassATTR(attr_value='attr1', attr_type='int', default_value=None),
ClassATTR(attr_value='attr2', attr_type='str', default_value="'default'")],
'AnotherClass': [ClassATTR(attr_value='attr3', attr_type='float', default_value=None)]}
Note:
parser.parsed_codeis an alias ofparser.output. Because the code is re-emitted from the AST, string literals are normalized (e.g."default"becomes'default').
What gets parsed
- Only top-level classes are reported; nested classes are ignored.
- An attribute is either an annotated assignment (
attr: type), whereattr_typeis the annotation, or a plain assignment (attr = value), whereattr_typeisNone.default_valueholds the default as it appears in the source, orNonewhen there is no default. - Docstrings, comments, methods and nested classes are ignored.
- Class-level decorators and PEP 695 type parameters (
class Box[T]:) are kept inparsed_code. - A duplicate top-level class name raises
ValueError.
Running the tests
uv sync
uv run pytest
uv run mypy pyclassparser
Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyclassparser-0.8.4.tar.gz.
File metadata
- Download URL: pyclassparser-0.8.4.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a4a46ab32eb4b5d189e4376363d502912f794da027c8f15358a09d7ea0f4e96
|
|
| MD5 |
768703da146554c238771c97ada71f0b
|
|
| BLAKE2b-256 |
2486f25772b137f3f6e202734897dacc4fe2497a19b1729e02a034df898c470b
|
Provenance
The following attestation bundles were made for pyclassparser-0.8.4.tar.gz:
Publisher:
release.yml on carlosplanchon/pyclassparser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyclassparser-0.8.4.tar.gz -
Subject digest:
2a4a46ab32eb4b5d189e4376363d502912f794da027c8f15358a09d7ea0f4e96 - Sigstore transparency entry: 2071689524
- Sigstore integration time:
-
Permalink:
carlosplanchon/pyclassparser@956196e4a2a6926b5488f06996890809067bb50f -
Branch / Tag:
refs/tags/v0.8.4 - Owner: https://github.com/carlosplanchon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@956196e4a2a6926b5488f06996890809067bb50f -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyclassparser-0.8.4-py3-none-any.whl.
File metadata
- Download URL: pyclassparser-0.8.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce9a92bb3d98c5cc81eaa3b6bf8338eb8da2ddbfb7a93502dbf42803dfd67ef1
|
|
| MD5 |
b13f88d63476262b9b633b9f0df28a3a
|
|
| BLAKE2b-256 |
8c8d07554ec72a01c257d8d291f352c2fef96ae07deff989881652b9b15e2c2b
|
Provenance
The following attestation bundles were made for pyclassparser-0.8.4-py3-none-any.whl:
Publisher:
release.yml on carlosplanchon/pyclassparser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyclassparser-0.8.4-py3-none-any.whl -
Subject digest:
ce9a92bb3d98c5cc81eaa3b6bf8338eb8da2ddbfb7a93502dbf42803dfd67ef1 - Sigstore transparency entry: 2071689534
- Sigstore integration time:
-
Permalink:
carlosplanchon/pyclassparser@956196e4a2a6926b5488f06996890809067bb50f -
Branch / Tag:
refs/tags/v0.8.4 - Owner: https://github.com/carlosplanchon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@956196e4a2a6926b5488f06996890809067bb50f -
Trigger Event:
push
-
Statement type: