Repository navigation
refactor: Improve Type Hinting and Static Analysis #71 - #96
Open
Yousa-Mirage wants to merge 10 commits into
Open
Yousa-Mirage wants to merge 10 commits into
Yousa-Mirage wants to merge 10 commits into
Conversation
…method` and `logical`
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.
Close #71
Description
This PR introduces comprehensive static type annotations to
pyalex/api.py. The primary goal is to resolve existing Pylance/MyPy errors, enable intelligent IDE autocompletion, and prevent type-related bugs during development, all without altering the existing runtime logic.The codebase now fully supports Python 3.8+ type checking standards, ensuring that entities like
Works,Authors, and configuration objects are correctly inferred by static analysis tools.Before
After
Please see the inlay hints of variable types:
Runtime Logic Integrity
AlexConfig) is preserved usingif not TYPE_CHECKINGblocks.Key Changes
Generic[T]):TypeVar("T")bound toOpenAlexEntity.BaseOpenAlex,OpenAlexResponseList, andPaginatorto be generic classes.Works().get()now correctly returnOpenAlexResponseList[Work], allowing IDEs to autocomplete methods specific toWork(e.g.,.ngrams(),.pdf) while iterating.AlexConfigHardening:email,api_key).__init__method. This allows static checkers to catch typos in configuration keys (e.g.,config.emaiil="...")will now raise a static error).if not TYPE_CHECKINGto hide the dynamic__getattr__/__setattr__methods from static analyzers while keeping them active at runtime.OpenAlexEntityandAlexConfigto inherit fromDict[str, Any]instead of plaindict.Literal["cursor", "page"]for pagination methods to prevent invalid string arguments.typing.castandOptionalto handle default values safely without confusing type checkers.pyalex/py.typedmarker file.pyproject.toml(specifically[tool.setuptools.package-data]) to include this file in the build distribution.pip install pyalex, their type checkers will automatically recognize and utilize the package's type hints.Benefits
Work,Author,Source, etc.).Union,Optionalinstead of|operator) to maintain the project's minimum version requirement.Verification
pytestpasses with no failures.api.pyand test scripts except the explicit type errorget(per_page="100")test.