Skip to content

Conversation

@aleexharris
Copy link
Contributor

@aleexharris aleexharris commented Oct 9, 2025

AsyncWeb3 Generic Type Fix

Problem

Closes #3759

Previously, when using AsyncWeb3 with a specific provider like AsyncHTTPProvider, type checkers (I found this issue using ty) could not resolve provider-specific methods because the AsyncWeb3.provider property was typed as returning AsyncBaseProvider rather than the concrete provider type.

This caused type checking errors like:

from aiohttp import ClientSession
from web3 import AsyncWeb3, AsyncHTTPProvider

async def example() -> None:
    endpoint_uri = "http://localhost:8545"
    w3 = AsyncWeb3(AsyncHTTPProvider(endpoint_uri))
    custom_session = ClientSession()
    
    # Type checker error: AsyncBaseProvider has no attribute 'cache_async_session'
    await w3.provider.cache_async_session(custom_session)
    w3.provider.disconnect()

Solution

The AsyncWeb3 class has been made generic using a TypeVar bound to AsyncBaseProvider. This allows type checkers to infer the specific provider type and resolve provider-specific methods correctly. This is purely a type annotation change with no impact on runtime behavior and is fully backwards compatible. All tests pass.

Implementation Changes

  1. Created a TypeVar bound to AsyncBaseProvider:
    AsyncProviderT = TypeVar("AsyncProviderT", bound=AsyncBaseProvider)
  2. Made AsyncWeb3 inherit from Generic[AsyncProviderT]
  3. Updated type annotations for the provider parameter and property to use AsyncProviderT

Example

The provider type is automatically inferred from the constructor argument (or can be explicitly annotated)

from web3 import AsyncWeb3
from web3.providers.rpc import AsyncHTTPProvider

async def example() -> None:
    # Type of w3 is inferred as AsyncWeb3[AsyncHTTPProvider]
    w3 = AsyncWeb3(AsyncHTTPProvider("http://localhost:8545"))

    # Explicitly annotate the type as AsyncWeb3[AsyncHTTPProvider]
    w3: AsyncWeb3[AsyncHTTPProvider] = AsyncWeb3(AsyncHTTPProvider("http://localhost:8545"))
    
    # Type checker now knows w3.provider is AsyncHTTPProvider
    # and can resolve AsyncHTTPProvider-specific methods
    from aiohttp import ClientSession
    custom_session = ClientSession()
    await w3.provider.cache_async_session(custom_session)
    
    w3.provider.disconnect()

Todo:

  • Clean up commit history
  • Add or update documentation related to these changes
  • Add entry to the release notes

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

Copy link
Collaborator

@fselmo fselmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aleexharris! This looks great and is definitely the preferred / correct approach. Much appreciated 🙏🏼.

I made some minor adjustments and some cleanup in the last commits here. @kclowes I'll let you sign off on this but lgtm 👍🏼

@aleexharris aleexharris changed the title AsyncWeb3 Generic Type Fix Make AsyncWeb3 Generic Oct 10, 2025
Copy link
Collaborator

@kclowes kclowes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, nice catch! I'd prefer to keep anything not related to the bug (casing/commas/whitespace/etc. changes) out of this PR if possible. I'm happy to update tomorrow or leave it for you, whatever you'd prefer!

Copy link
Collaborator

@kclowes kclowes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, a lot of what I thought was random changes was black fixups. LGTM!

@kclowes kclowes merged commit 2dced98 into ethereum:main Oct 16, 2025
85 checks passed
BobTheBuidler pushed a commit to BobTheBuidler/faster-web3.py that referenced this pull request Oct 17, 2025
* fix: Made function params Optional, as None is the default input

* feat: Made AsyncWeb3 class Generic to fix type errors when importing Web3

* refactor: remove unnecessary noqa: E501 uses

* fix: remove `Optional` where it isn't optional

* fix: Use `Optional` where appropriate

* chore: add newsfragment for ethereum#3761

* fix: Removed  where it isn't optional

* Remove unnecessary changes

---------

Co-authored-by: fselmo <fselmo2@gmail.com>
Co-authored-by: kclowes <kclowes@users.noreply.github.com>
BobTheBuidler pushed a commit to BobTheBuidler/faster-web3.py that referenced this pull request Oct 17, 2025
* fix: Made function params Optional, as None is the default input

* feat: Made AsyncWeb3 class Generic to fix type errors when importing Web3

* refactor: remove unnecessary noqa: E501 uses

* fix: remove `Optional` where it isn't optional

* fix: Use `Optional` where appropriate

* chore: add newsfragment for ethereum#3761

* fix: Removed  where it isn't optional

* Remove unnecessary changes

---------

Co-authored-by: fselmo <fselmo2@gmail.com>
Co-authored-by: kclowes <kclowes@users.noreply.github.com>
BobTheBuidler added a commit to BobTheBuidler/faster-web3.py that referenced this pull request Oct 17, 2025
* Make AsyncWeb3 Generic (ethereum#3761)

* fix: Made function params Optional, as None is the default input

* feat: Made AsyncWeb3 class Generic to fix type errors when importing Web3

* refactor: remove unnecessary noqa: E501 uses

* fix: remove `Optional` where it isn't optional

* fix: Use `Optional` where appropriate

* chore: add newsfragment for ethereum#3761

* fix: Removed  where it isn't optional

* Remove unnecessary changes

---------

Co-authored-by: fselmo <fselmo2@gmail.com>
Co-authored-by: kclowes <kclowes@users.noreply.github.com>

* Compile release notes for v7.14.0

* Bump version: 7.13.0 → 7.14.0

* Align isort skip path

---------

Co-authored-by: Alex Harris <122488678+aleexharris@users.noreply.github.com>
Co-authored-by: fselmo <fselmo2@gmail.com>
Co-authored-by: kclowes <kclowes@users.noreply.github.com>
Co-authored-by: Shresth79 <shresthpatelshrepate@gmail.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.

Make AsyncWeb3 class generic to fix example from docs

3 participants