Dev-iL commented on PR #1376:
URL: https://github.com/apache/hamilton/pull/1376#issuecomment-3907237932

   Some suggestions by Claude:
   
   # Dependency & Structure Improvements for Hamilton
   
   ## 1. Remove `typing_inspect` dependency
   
   `typing_inspect` is used in 6 files but all its functions have stdlib 
equivalents (Python 3.8+):
   
   | `typing_inspect` function | Replacement |
   |---|---|
   | `get_origin()` / `get_args()` | Already handled in `hamilton/htypes.py` 
via `typing.get_origin` / `typing_extensions.get_origin` |
   | `is_optional_type(tp)` | `get_origin(tp) is Union and type(None) in 
get_args(tp)` |
   | `is_union_type(tp)` | `get_origin(tp) is Union` |
   | `is_generic_type(tp)` | `get_origin(tp) is not None` |
   | `is_tuple_type(tp)` | `get_origin(tp) is tuple` |
   | `is_typevar(tp)` | `isinstance(tp, TypeVar)` |
   | `is_literal_type(tp)` | `get_origin(tp) is Literal` |
   
   These are used across `hamilton/htypes.py`, `hamilton/node.py`, 
`hamilton/function_modifiers/dependencies.py`, 
`hamilton/function_modifiers/expanders.py`, 
`hamilton/function_modifiers/adapters.py`, and 
`hamilton/experimental/h_cache.py`. A small helper module (or additions to 
`htypes.py`) could centralize these ~5 one-liner functions and eliminate the 
dependency entirely.
   
   **Impact**: Removes 1 of 4 required dependencies.
   
   ---
   
   ## 2. Make remaining top-level pandas/numpy imports lazy
   
   `base.py` was already fixed, but 3 files still have top-level imports:
   
   | File | Import | Fix |
   |---|---|---|
   | `hamilton/driver.py:48` | `import pandas as pd` | Move inside the 
functions that use it (it's only used in the `__main__` block) |
   | `hamilton/log_setup.py:21` | `import numpy as np` | Move inside 
`setup_logging()` — it's only used on line 47 for `np.seterr()` |
   | `hamilton/models.py:21` | `import pandas as pd` | Move to `TYPE_CHECKING` 
block — only used for type annotations in `predict()` signature |
   
   These are the remaining blockers for `hamilton-core` working cleanly without 
pandas/numpy installed. Without these fixes, `import hamilton.driver` or 
`import hamilton.log_setup` will crash in a `sf-hamilton-core` environment.
   
   ---
   
   ## 3. `typing_extensions` — keep for now
   
   This dependency is minimal (used for `Annotated`, `NotRequired`, 
`get_origin`, `get_args`, `is_typeddict` — all backports for Python < 3.11). 
It's lightweight and can't be removed until the minimum Python version is 
bumped to 3.11+. The current constraint `> 4.0.0` is correct.
   
   **Minor fix needed**: `hamilton/function_modifiers/expanders.py:762` imports 
`typing_extensions.is_typeddict` unconditionally — should add a version check 
like the other imports:
   
   ```python
   if sys.version_info >= (3, 10):
       from typing import is_typeddict
   else:
       from typing_extensions import is_typeddict
   ```
   
   ---
   
   ## 4. Improve the hamilton-core build with uv workspaces
   
   The current `hamilton-core/setup.py` copies the entire `hamilton/` source 
tree into `hamilton/_hamilton/` at build time via `shutil.copytree`. This works 
but is fragile:
   
   - Stale copies if setup.py isn't re-run
   - `_hamilton/` is `.gitignore`d, so debugging the installed package is 
confusing
   - The proxy module in `hamilton-core/hamilton/__init__.py` adds runtime 
overhead and complexity
   
   **Better approach with uv workspaces**:
   
   ```toml
   # Root pyproject.toml — add workspace config
   [tool.uv.workspace]
   members = ["hamilton-core"]
   
   # hamilton-core/pyproject.toml — replace setup.py entirely
   [project]
   name = "sf-hamilton-core"
   dynamic = ["version"]
   dependencies = [
       "typing_extensions > 4.0.0",
       # no pandas, numpy, or typing_inspect
   ]
   
   [tool.uv.sources]
   sf-hamilton = { workspace = true }
   ```
   
   This eliminates the copy step, the proxy module, and the dynamic `setup.py` 
entirely. The `hamilton` package is referenced directly from the workspace root.
   
   ---
   
   ## 5. Use prek workspace mode for pre-commit hooks
   
   With `prek` now on main, you can use its workspace feature to manage hooks 
per-package:
   
   ```yaml
   # hamilton-core/.pre-commit-config.yaml
   orphan: true  # isolate from root hooks
   repos:
     - repo: https://github.com/astral-sh/ruff-pre-commit
       rev: v0.15.1
       hooks:
         - id: ruff-check
         - id: ruff-format
   ```
   
   `prek` auto-discovers this config and scopes the hooks to `hamilton-core/` 
files only. This keeps linting/formatting consistent while allowing each 
sub-package to customize.
   
   ---
   
   ## 6. Drop `networkx` from visualization extra
   
   The current main `pyproject.toml` lists `networkx` as part of the 
`visualization` extra, but the `hamilton-core` setup.py already drops it:
   
   ```python
   **{"visualization": ["graphviz"]},  # drop networkx
   ```
   
   If networkx is genuinely unused in the visualization code path, this change 
should be applied to the main `pyproject.toml` as well.
   
   ---
   
   ## Summary — final dependency picture
   
   | | `sf-hamilton` (current) | `sf-hamilton-core` (current) | After proposed 
changes |
   |---|---|---|---|
   | `pandas` | required | removed | removed |
   | `numpy` | required | removed | removed |
   | `typing_inspect` | required | required | **removed** |
   | `typing_extensions` | required | required | required (until Python 3.11 
min) |
   | **Total core deps** | **4** | **2** | **1** |
   
   The single remaining hard dependency would be `typing_extensions`, and even 
that goes away once Python < 3.11 is dropped. At that point, `sf-hamilton-core` 
would have **zero** third-party dependencies.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to