Add Python type annotations and adopt standard src layout#1
Draft
greggdonovan wants to merge 3 commits intomasterfrom
Draft
Add Python type annotations and adopt standard src layout#1greggdonovan wants to merge 3 commits intomasterfrom
greggdonovan wants to merge 3 commits intomasterfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib/py(moving files intosrc/thrift/directory structure)py.typedmarker file for PEP 561 compliancepyproject.tomlwith modern Python packaging configurationTest plan
🤖 Generated with Claude Code