A @nodiscard decorator and static checker for Python — detect when return values of marked methods are silently discarded
Project description
nodiscard
A @nodiscard decorator and static checker for Python — like Rust's #[must_use] or C++'s [[nodiscard]].
Problem
Frozen/immutable models return new instances from mutation methods. Forgetting to capture the return value is a silent no-op:
user = User(name="Alice")
user.rename("Bob") # Bug! Returns a new User, but the result is silently discarded.
print(user.name) # Still "Alice"
Quick Start
pip install nodiscard
from nodiscard import nodiscard
class User:
@nodiscard
def rename(self, name: str) -> "User":
return User(name=name)
nodiscard check src/
# src/app.py:42:5: ND001 Return value of '@nodiscard' method 'rename' is discarded
Usage
Decorator
from nodiscard import nodiscard, NoDiscard
from typing import Annotated
class Schema:
@nodiscard
def merge(self, other: "Schema") -> "Schema": ...
# Alternative: Annotated marker
def replace(self, **kwargs: object) -> Annotated["Schema", NoDiscard]: ...
Works with @classmethod, @staticmethod, @abstractmethod, async def, and decorator stacking.
CLI
nodiscard check src/ # Check a directory
nodiscard check src/models.py # Check a single file
nodiscard check src/ --src src/ # Set import resolution root
nodiscard check src/ --exclude "tests/*" # Exclude patterns
nodiscard check src/ --format json # JSON output
Exit codes: 0 = no violations, 1 = violations found, 2 = error (e.g. path not found).
Configuration
# pyproject.toml
[tool.nodiscard]
src = ["src"]
exclude = ["tests/*", "migrations/*"]
format = "text" # "text" or "json"
CLI arguments override pyproject.toml settings.
Rules
| Code | Name | Description |
|---|---|---|
| ND001 | discarded-nodiscard-return | Return value of a @nodiscard method is discarded |
Inline suppression
obj.method() # nodiscard: ignore
pre-commit
# .pre-commit-config.yaml
repos:
- repo: https://github.com/KinjiKawaguchi/nodiscard
rev: v0.1.0
hooks:
- id: nodiscard
args: [check, src/]
Comparison
| Feature | Rust #[must_use] |
C++ [[nodiscard]] |
Python @nodiscard |
|---|---|---|---|
| Compile-time | Yes | Yes | Static analysis |
| Functions | Yes | Yes | Methods (v0.1) |
| Custom message | Yes | C++20 | @nodiscard(reason="...") |
| Suppressible | let _ = ... |
(void)expr |
_ = expr / # nodiscard: ignore |
Limitations
- v0.1 detects method calls only (not bare function calls)
- Type inference is local-scope only (no full type resolution like mypy/pyright)
- No IDE integration yet (LSP planned for future)
asynctask tracking (asyncio.create_task) is not analyzed
Contributing
- DeepWiki — AI-generated codebase documentation
git clone https://github.com/KinjiKawaguchi/nodiscard.git
cd nodiscard
uv sync
uv run pytest --cov=nodiscard
uv run ruff check src/ tests/
License
MIT
Project details
Release history Release notifications | RSS feed
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 nodiscard-0.1.0.tar.gz.
File metadata
- Download URL: nodiscard-0.1.0.tar.gz
- Upload date:
- Size: 101.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac8c6c805d41b390f7b079d2060791ded2799c52516b9efc544484dd678ea865
|
|
| MD5 |
ee26c53c743d4e6c789a67261b73c5b1
|
|
| BLAKE2b-256 |
f6e46e010d3b62dd3b459b8e138db41b87ba735df90693138bc2fd011333b0d1
|
Provenance
The following attestation bundles were made for nodiscard-0.1.0.tar.gz:
Publisher:
release.yml on KinjiKawaguchi/nodiscard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nodiscard-0.1.0.tar.gz -
Subject digest:
ac8c6c805d41b390f7b079d2060791ded2799c52516b9efc544484dd678ea865 - Sigstore transparency entry: 1242555419
- Sigstore integration time:
-
Permalink:
KinjiKawaguchi/nodiscard@73f6be89afa4d6a760493309467c1317abb0c172 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KinjiKawaguchi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@73f6be89afa4d6a760493309467c1317abb0c172 -
Trigger Event:
push
-
Statement type:
File details
Details for the file nodiscard-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nodiscard-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
671b698aca12287eb9528244e9e1eb2c4c9b6484fa9ba55bc777a2f1850352f8
|
|
| MD5 |
5903f2b41c93c256a19ac20f1fc707dc
|
|
| BLAKE2b-256 |
2379661b05936062c810490a6fa9b8fb68aef8825f7ef97b2a48708b477783cb
|
Provenance
The following attestation bundles were made for nodiscard-0.1.0-py3-none-any.whl:
Publisher:
release.yml on KinjiKawaguchi/nodiscard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nodiscard-0.1.0-py3-none-any.whl -
Subject digest:
671b698aca12287eb9528244e9e1eb2c4c9b6484fa9ba55bc777a2f1850352f8 - Sigstore transparency entry: 1242555420
- Sigstore integration time:
-
Permalink:
KinjiKawaguchi/nodiscard@73f6be89afa4d6a760493309467c1317abb0c172 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KinjiKawaguchi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@73f6be89afa4d6a760493309467c1317abb0c172 -
Trigger Event:
push
-
Statement type: