Skip to content

Ensure runtime type-checking with beartype #143

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

Merged
merged 108 commits into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
5d4ecc3
adding beartype to dependencies
adamamer20 Mar 22, 2025
7386865
replacing typeguard with beartype
adamamer20 Mar 22, 2025
1153168
adding uv lock
adamamer20 Mar 22, 2025
0cac617
adding uv lock
adamamer20 Apr 1, 2025
e08e086
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 1, 2025
454382b
adding beartype to dependencies
adamamer20 Mar 22, 2025
977f45d
replacing typeguard with beartype
adamamer20 Mar 22, 2025
c6eccc4
adding uv lock
adamamer20 Apr 1, 2025
2cc81cd
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 1, 2025
1d660da
adding beartype decorator to classes
adamamer20 Apr 1, 2025
9e9b62f
Solving cyclic imports
adamamer20 Apr 1, 2025
84d9ada
Merge branch 'main' of https://github.com/projectmesa/mesa-frames int…
adamamer20 Apr 3, 2025
6c7d405
fix: Method mesa_frames.concrete.pandas.mixin.PandasMixin._df_constru…
adamamer20 Apr 3, 2025
1379500
Merge branch 'main' of https://github.com/projectmesa/mesa-frames int…
adamamer20 Apr 3, 2025
4f1dd08
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 4, 2025
f2cd30b
Merge branch 'main' of https://github.com/projectmesa/mesa-frames int…
adamamer20 Apr 11, 2025
44085d3
adding missing idslike to agentset
adamamer20 Apr 11, 2025
24da495
update
adamamer20 Apr 18, 2025
6b343b2
Merge branch 'main' into beartype
adamamer20 Apr 22, 2025
54b906c
correcting type hint
adamamer20 Apr 22, 2025
c31eade
correcting type hint
adamamer20 Apr 22, 2025
577da2b
correcting type hint
adamamer20 Apr 22, 2025
3be6d48
converting numpy to int
adamamer20 Apr 22, 2025
2545c75
updating uv lock
adamamer20 Apr 22, 2025
d91e54a
adding expr to bool series
adamamer20 Apr 22, 2025
6ac1463
stringifying future imports
adamamer20 Apr 22, 2025
3a95c85
AgentSetDF not type[AgentSetDF]
adamamer20 Apr 22, 2025
b1650d2
stringifying type
adamamer20 Apr 22, 2025
6e94ef0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 22, 2025
7ef57ec
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 23, 2025
519705d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2025
4899567
correcting type hints
adamamer20 Apr 23, 2025
5f17d62
adding new and necessary types
adamamer20 Apr 23, 2025
68560dd
llm rules for agentic llm use
adamamer20 Apr 23, 2025
1ec29e0
correcting type hints
adamamer20 Apr 23, 2025
d787902
changing type hints for mixin
adamamer20 Apr 23, 2025
578ca42
correcting type hints for mixin
adamamer20 Apr 23, 2025
7214b4e
correcting type hnts for space
adamamer20 Apr 23, 2025
aa4e0fc
refactor: reorder import statements for clarity
adamamer20 Apr 24, 2025
981fbb9
fix: update type hints to include Series for reindexing methods
adamamer20 Apr 24, 2025
1fff53a
refactor: update type hints and remove unused aliases in agents and a…
adamamer20 Apr 24, 2025
1185f40
fix: update type hint for agents parameter in remove_agents method
adamamer20 Apr 24, 2025
c194dc4
fix: update type hints to include DataFrame for discard and remove me…
adamamer20 Apr 24, 2025
1b2e089
fix: update type hints to include Literal["active"] for agents parame…
adamamer20 Apr 24, 2025
5ee1730
fix: update return type hint for space property to include None
adamamer20 Apr 24, 2025
8223ae7
fix: add check for None before removing agents in AgentsDF class
adamamer20 Apr 24, 2025
09f72d6
fix: update type hints to include Collection[Any] for agent parameter…
adamamer20 Apr 24, 2025
640d914
fix: update type hints to include DataFrame for agent removal methods…
adamamer20 Apr 24, 2025
9e7d257
fix: replace pl.lit with pl.Series for unique_id in _check_ids_presen…
adamamer20 Apr 24, 2025
0023ef0
fix: update type hints to include tuple of AgentMask and Collection[s…
adamamer20 Apr 24, 2025
d76f6db
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 24, 2025
029aeb5
fix: remove unused imports in agents.py for cleaner code
adamamer20 Apr 24, 2025
a0f278f
fix: update type hints and imports in types_.py for improved clarity …
adamamer20 Apr 24, 2025
312c4d5
fix: update type hint for agents parameter in discard method to inclu…
adamamer20 Apr 24, 2025
65d95ae
fix: enhance type hints and add Pandas and ArrowTable support in type…
adamamer20 Apr 24, 2025
1901e34
fix: remove unused imports in types_.py for cleaner code
adamamer20 Apr 24, 2025
867a1b1
fix: refine type hints in AgentContainer class for improved type safe…
adamamer20 Apr 24, 2025
e2a39ac
fix: update type hints in AgentContainer class for improved type safe…
adamamer20 Apr 24, 2025
f24267c
fix: enhance type hints in AgentContainer and types_ for improved typ…
adamamer20 Apr 24, 2025
f46b9bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 24, 2025
7fee27e
feat: add discard method to AgentSetDF for agent removal without error
adamamer20 Apr 25, 2025
c07df58
fix: update type hint for remove method in AgentSetDF to use AgentMas…
adamamer20 Apr 25, 2025
073bc13
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 25, 2025
fb82772
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2025
1dad926
fix: update Polars type aliases for consistency and clarity
adamamer20 Apr 25, 2025
f10d4f5
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 25, 2025
a1142ac
fix: add pre-commit as a dependency for improved code quality checks
adamamer20 Apr 25, 2025
c1589b8
fix: update DataFrameInput type in DataFrameMixin for improved type s…
adamamer20 Apr 25, 2025
a239a2e
fix: remove pre-commit dependency from pyproject.toml
adamamer20 Apr 25, 2025
e21e377
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2025
8450d02
fix: update DataFrameMixin method signatures for improved type consis…
adamamer20 Apr 25, 2025
eb35df7
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 25, 2025
8f60fb3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2025
b29637f
fix: update properties type in DiscreteSpaceDF for improved type safety
adamamer20 Apr 25, 2025
a958eaf
fix: replace typing_extensions with typing for improved compatibility
adamamer20 Apr 25, 2025
a5f1be0
Merge branch 'beartype' of https://github.com/projectmesa/mesa-frames…
adamamer20 Apr 25, 2025
e453ebf
fix: remove DataFrame type from agent removal methods for improved ty…
adamamer20 Apr 25, 2025
af74bf4
refactor: remove overloaded discard method signatures for cleaner imp…
adamamer20 Apr 25, 2025
aff2477
fix: update _mask type in AgentSetPolars for improved type flexibility
adamamer20 Apr 25, 2025
fd61803
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2025
d600342
fix: update agent position retrieval for improved data handling
adamamer20 Apr 25, 2025
b0a5527
fix: update _df_index method signatures for improved type flexibility
adamamer20 Apr 25, 2025
f994142
fix: streamline agent removal logic and enhance space property valida…
adamamer20 Apr 25, 2025
5df435b
fix: update space setter to require SpaceDF type for improved type sa…
adamamer20 Apr 25, 2025
8d6b623
fix: replace math.inf with imported inf for consistency and clarity
adamamer20 Apr 25, 2025
de7da65
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2025
a82ca03
fix: update Python version requirements in build configuration
adamamer20 Apr 29, 2025
763367e
fix: update pyupgrade argument to support Python 3.11
adamamer20 Apr 29, 2025
21ed4e8
fix: remove unused DataFrame import from mixin.py
adamamer20 Apr 29, 2025
14d32cb
fix: update type hint in AgentContainer to improve clarity
adamamer20 Apr 29, 2025
886f064
mesa version has to be fixed to 2.3.4 to manage manual assignment to …
adamamer20 Apr 29, 2025
f679be6
updating the uv lock
adamamer20 Apr 29, 2025
e183449
Accept our uv.lock
adamamer20 Apr 29, 2025
d1a5ed1
select lost the abstract method decorator
adamamer20 Apr 29, 2025
74773e2
Fix if space is none mesa_frames/abstract/agents.py
adamamer20 Apr 29, 2025
a1189d2
extra parenthesis
adamamer20 Apr 29, 2025
b880ecd
Merge branch 'main' of https://github.com/projectmesa/mesa-frames int…
adamamer20 May 15, 2025
2c4faf2
Merge branch 'main' of https://github.com/projectmesa/mesa-frames int…
adamamer20 Jun 1, 2025
c163e7d
update dependencies
adamamer20 Jun 1, 2025
af5793c
fix: update Python version requirements to >=3.11 and adjust resoluti…
adamamer20 Jun 1, 2025
019c7d1
refactor: remove beartype decorators from various classes and imports
adamamer20 Jun 1, 2025
aec98db
feat: add runtime type checking support in tests with beartype warning
adamamer20 Jun 1, 2025
d69ac72
fix: remove redundant text in warning for missing beartype installation
adamamer20 Jun 1, 2025
19e7132
fix: remove redundant space in warning message for missing beartype i…
adamamer20 Jun 1, 2025
b18d646
refactor: simplify dictionary initialization in AgentsDF class
adamamer20 Jun 1, 2025
ab4038a
docs: add runtime type checking section to development guidelines and…
adamamer20 Jun 1, 2025
d5d5a0b
feat: enhance runtime type checking setup in documentation and config…
adamamer20 Jun 1, 2025
3841216
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ jobs:
fail-fast: False
matrix:
os: [windows, ubuntu, macos]
python-version: ["3.12"]
python-version: ["3.13"]
include:
- os: ubuntu
python-version: "3.11"
- os: ubuntu
python-version: "3.10"
python-version: "3.12"
# Disabled for now. See https://github.com/projectmesa/mesa/issues/1253
#- os: ubuntu
# python-version: 'pypy-3.8'
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@ cython_debug/
.idea/

.vscode/
*.code-workspace
*.code-workspace
llm_rules.md
.python-version
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
rev: v3.19.1
hooks:
- id: pyupgrade
args: [--py310-plus]
args: [--py311-plus]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
Expand Down
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ It is recommended to set up a virtual environment before installing dependencies

- If using UV: `uv run pytest --cov`

- **Optional: Enable runtime type checking** during development for enhanced type safety:

```sh
MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest --cov
```

!!! tip "Automatic with Hatch"
If you're using Hatch development environment (`hatch shell dev`), runtime type checking is automatically enabled. No manual setup needed!

For more details on runtime type checking, see the [Development Guidelines](https://projectmesa.github.io/mesa-frames/development/).

#### **Step 6: Documentation Updates (If Needed)** 📖

- If you add a new feature, update the documentation accordingly.
Expand Down
147 changes: 145 additions & 2 deletions docs/general/development/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,147 @@
# Development Guidelines

!!! warning
This page is in construction, check it again soon.
## Runtime Type Checking 🔍

mesa-frames includes optional runtime type checking using [beartype](https://github.com/beartype/beartype) for development and debugging purposes. This feature helps catch type-related errors early during development and testing.

!!! tip "Automatic for Hatch Development Environment"
When using Hatch for development (`hatch shell dev`), runtime type checking is **automatically enabled** by default. No manual setup required!

### Development Environment Setup

#### Option 1: Hatch Development Environment (Recommended)

The easiest way to enable runtime type checking is to use Hatch's development environment:

```bash
# Enter the development environment (auto-enables runtime type checking)
hatch shell dev

# Verify it's enabled
python -c "import os; print('Runtime type checking:', os.getenv('MESA_FRAMES_RUNTIME_TYPECHECKING'))"
# → Runtime type checking: true
```

#### Option 2: Manual Environment Variable

For other development setups, you can manually enable runtime type checking:

Runtime type checking can be enabled by setting the `MESA_FRAMES_RUNTIME_TYPECHECKING` environment variable:

```bash
export MESA_FRAMES_RUNTIME_TYPECHECKING=1
# or
export MESA_FRAMES_RUNTIME_TYPECHECKING=true
# or
export MESA_FRAMES_RUNTIME_TYPECHECKING=yes
```

### Usage Examples

#### For Development and Testing

```bash
# Enable runtime type checking for testing
MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run pytest

# Enable runtime type checking for running scripts
MESA_FRAMES_RUNTIME_TYPECHECKING=1 uv run python your_script.py
```

#### In Your IDE or Development Environment

Add the environment variable to your development environment configuration:

=== "VS Code"
In your `.vscode/settings.json`:
```json
{
"python.env": {
"MESA_FRAMES_RUNTIME_TYPECHECKING": "1"
}
}
```

=== "PyCharm"
In your run configuration, add the environment variable:
```
MESA_FRAMES_RUNTIME_TYPECHECKING=1
```

### How It Works

When enabled, the runtime type checking system:

1. **Automatically instruments** all mesa-frames packages with beartype decorators
2. **Validates function arguments** and return values at runtime
3. **Provides detailed error messages** when type mismatches occur
4. **Helps catch type-related bugs** during development

### Requirements

Runtime type checking requires the optional `beartype` dependency:

```bash
# Install beartype for runtime type checking
uv add beartype
# or
pip install beartype
```

!!! note "Optional Dependency"
If `beartype` is not installed and runtime type checking is enabled, mesa-frames will issue a warning and continue without type checking.

### Performance Considerations

!!! warning "Development Only"
Runtime type checking adds significant overhead and should **only be used during development and testing**. Do not enable it in production environments.

The overhead includes:

- Function call interception and validation
- Type checking computations at runtime
- Memory usage for type checking infrastructure

### When to Use Runtime Type Checking

✅ **Recommended for:**

- Development and debugging
- Writing new features
- Running unit tests
- Troubleshooting type-related issues
- Contributing to mesa-frames

❌ **Not recommended for:**

- Production deployments
- Performance benchmarking
- Large-scale simulations
- Final model runs

### Troubleshooting

If you encounter issues with runtime type checking:

1. **Check beartype installation:**

```bash
uv run python -c "import beartype; print(beartype.__version__)"
```

2. **Verify environment variable:**

```bash
echo $MESA_FRAMES_RUNTIME_TYPECHECKING
```

3. **Check for warnings** in your application logs

4. **Disable temporarily** if needed:

```bash
unset MESA_FRAMES_RUNTIME_TYPECHECKING
```

!!! tip "Pro Tip"
Runtime type checking is particularly useful when developing custom AgentSet implementations or working with complex DataFrame operations where type safety is crucial.
21 changes: 20 additions & 1 deletion mesa_frames/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,28 @@ def __init__(self, width, height):
GitHub: https://github.com/projectmesa/mesa-frames
"""

from __future__ import annotations

import os

# Enable runtime type checking if requested via environment variable
if os.getenv("MESA_FRAMES_RUNTIME_TYPECHECKING", "").lower() in ("1", "true", "yes"):
try:
from beartype.claw import beartype_this_package

beartype_this_package()
except ImportError:
import warnings

warnings.warn(
"MESA_FRAMES_RUNTIME_TYPECHECKING is enabled but beartype is not installed.",
ImportWarning,
stacklevel=2,
)

from mesa_frames.concrete.agents import AgentsDF
from mesa_frames.concrete.model import ModelDF
from mesa_frames.concrete.agentset import AgentSetPolars
from mesa_frames.concrete.model import ModelDF
from mesa_frames.concrete.space import GridPolars

__all__ = [
Expand Down
Loading