Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An asynccontextmanager method breaks __init__ inference for attrs classes #1586

Open
gmacon opened this issue Feb 14, 2024 · 0 comments
Open
Labels
bug cat: stubs and 3p type stubs and third-party types

Comments

@gmacon
Copy link

gmacon commented Feb 14, 2024

Pytype 2024.02.13
Python 3.8.18

This file:

from contextlib import asynccontextmanager
from typing import AsyncGenerator

from attrs import define


@define
class Thing:
    instance: int

    @asynccontextmanager
    async def context(self) -> AsyncGenerator[None, None]:
        yield

produces this output from pytype-single --output - repro.py:

import attr
import contextlib
from typing import Annotated, AsyncIterator, Callable, ParamSpec, TypeVar

define: Annotated[Callable, 'pytype_metadata', {'tag': 'attr.s', 'init': True, 'kw_only': False, 'auto_attribs': None}]

_P = ParamSpec('_P')
_T_co = TypeVar('_T_co')

@attr.s
class Thing:
    instance: int
    def __init__(self) -> None: ...
    def context(self) -> contextlib._AsyncGeneratorContextManager[None]: ...

def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, contextlib._AsyncGeneratorContextManager[_T_co]]: ...

Note __init__(self) -> None: ... is incorrect, should be __init__(self, instance: int) -> None: ...

If you remove `context:

from contextlib import asynccontextmanager
from typing import AsyncGenerator

from attrs import define


@define
class Thing:
    instance: int

this gives:

import attr
import contextlib
from typing import Annotated, AsyncIterator, Callable, ParamSpec, TypeVar

define: Annotated[Callable, 'pytype_metadata', {'tag': 'attr.s', 'init': True, 'kw_only': False, 'auto_attribs': None}]

_P = ParamSpec('_P')
_T_co = TypeVar('_T_co')

@attr.s
class Thing:
    instance: int
    def __init__(self, instance: int) -> None: ...

def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, contextlib._AsyncGeneratorContextManager[_T_co]]: ...

which has the correct signature for __init__.

@rchen152 rchen152 added bug cat: stubs and 3p type stubs and third-party types labels Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: stubs and 3p type stubs and third-party types
Projects
None yet
Development

No branches or pull requests

2 participants