Skip to content

Add Python type annotations and adopt standard src layout#1

Draft
greggdonovan wants to merge 3 commits intomasterfrom
python-type-annotations
Draft

Add Python type annotations and adopt standard src layout#1
greggdonovan wants to merge 3 commits intomasterfrom
python-type-annotations

Conversation

@greggdonovan
Copy link
Owner

Summary

  • Adopt standard Python src layout for lib/py (moving files into src/thrift/ directory structure)
  • Add comprehensive type annotations throughout the Python library
  • Add py.typed marker file for PEP 561 compliance
  • Add pyproject.toml with modern Python packaging configuration
  • Fix C extension build on macOS (endian.h compatibility)

Test plan

  • Run existing Python test suite
  • Verify type checking with mypy
  • Test C extension build on macOS and Linux
  • Verify backward compatibility with existing code using the library

🤖 Generated with Claude Code

greggdonovan and others added 3 commits December 22, 2025 10:40
This commit migrates lib/py from a non-standard package layout to the
modern Python "src layout" pattern while preserving full API compatibility.

## Background

The previous layout used a `package_dir={'thrift': 'src'}` mapping in
setup.py, which mapped the `thrift` package to the `src/` directory.
While functional, this approach:

- Required workarounds for type checkers (`extra-paths` in pyproject.toml)
- Confused some IDEs and tooling that expect standard layouts
- Made editable installs less intuitive

## Changes

### Directory Structure

Moved all package contents from `src/` to `src/thrift/`:

```
Before:                          After:
lib/py/src/                      lib/py/src/thrift/
├── __init__.py                  ├── __init__.py
├── Thrift.py                    ├── Thrift.py
├── protocol/                    ├── protocol/
├── transport/                   ├── transport/
├── server/                      ├── server/
└── ext/                         └── ext/
```

### Build Configuration

- setup.py: Changed `package_dir={'thrift': 'src'}` to `package_dir={'': 'src'}`
- setup.py: Updated C extension include_dirs from `src` to `src/thrift`
- pyproject.toml: Removed `extra-paths` workaround for type checker
- pyproject.toml: Added pytest configuration

### Type Checking Fixes

The standard layout enabled proper import resolution, which revealed
several type errors that were previously hidden:

1. **Parameter name mismatches** (Liskov Substitution Principle):
   - TJSONProtocol: `binary`→`str_val`, `dbl`→`dub`, `type`→`ttype`
   - TMultiplexedProtocol: `type`→`ttype`

2. **Type compatibility fixes**:
   - TCompactProtocol: `writeVarint` now accepts `TTransportBase | BytesIO`
   - THttpServer: Added `cast(BinaryIO, self.rfile)` for type safety
   - TTransport: Added assertion for SASL mechanism
   - TNonblockingServer: Added null check for client.handle

## API Compatibility

Client code remains unchanged - all imports work as before:

```python
from thrift.Thrift import TException
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket
```

## Testing

- Type checker: 0 errors (3 warnings for optional attributes)
- pytest: 4 passed, 11 skipped (SSL tests require test resources)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
On macOS, <netinet/in.h> includes <sys/_endian.h> which already defines
ntohll and htonll as macros:

    #define ntohll(x) __DARWIN_OSSwapInt64(x)
    #define htonll(x) __DARWIN_OSSwapInt64(x)

When we tried to define functions with these names, the macros would
expand, causing syntax errors like:

    error: expected ')'
    static inline unsigned long long ntohll(...)
                                     ^
    note: expanded from macro 'ntohll'

The fix wraps our ntohll/htonll definitions in #ifndef guards so they're
only defined when not already provided by system headers. This allows
the C extension (fastbinary) to build successfully on macOS while
maintaining compatibility with other platforms.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Changed gen_type_hints_ default from false to true
- Removed requirement for enum option when using type_hints
- Added no_type_hints option to disable when needed
- Updated Optional[T] syntax to modern T | None (Python 3.10+)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant