Skip to content

False positive not-async-context-manager with Pydantic PrivateAttr #9813

Open
@jamesbraza

Description

@jamesbraza

Bug description

There is a false positive not-async-context-manager when using Pydantic v2's PrivateAttr. Fyi I am coming from pydantic/pydantic#9921

import asyncio

from pydantic import BaseModel, PrivateAttr


class SomeModel(BaseModel):
    _some_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)

    async def some_method(self) -> None:
        async with self._some_lock:
            await asyncio.sleep(1)


model = SomeModel()
asyncio.run(model.some_method())

Configuration

load-plugins = "pylint_pydantic"

Command used

pylint a.py

Pylint output

a.py:10:8: E1701: Async context manager 'ModelPrivateAttr' doesn't implement __aenter__ and __aexit__. (not-async-context-manager)

Expected behavior

No message

Pylint version

pylint 3.2.5
astroid 3.2.3
Python 3.12.4 (main, Jul  3 2024, 13:35:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]

OS / Environment

macOS Sonoma version 14.3

Additional dependencies

pydantic version: 2.8.2
        pydantic-core version: 2.20.1
          pydantic-core build: profile=release pgo=true
                 install path: /path/to/venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.4 (main, Jul  3 2024, 13:35:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.3-arm64-arm-64bit
             related packages: mypy-1.10.1 typing_extensions-4.12.2 fastapi-0.111.1
                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeLib specific 💅This affect the code from a particular libraryNeeds astroid Brain 🧠Needs a brain tip in astroid (then an astroid upgrade)Needs astroid updateNeeds an astroid update (probably a release too) before being mergable

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions