flake8-pie icon indicating copy to clipboard operation
flake8-pie copied to clipboard

prefer-with

Open sbdchd opened this issue 4 years ago • 0 comments

Basically, for things like DB clients, prefer using the with API instead of manually calling close

# err
conn = psycopg2.connect(dsn=dsn)
cursor = conn.cursor()
cursor.execute("SELECT 1")

cursor.close()
conn.close()

# ok
with psycopg2.connect(dsn=dsn) as conn, conn.cursor() as cursor:
    cursor.execute("SELECT 1")

I think this requires type information since we need to check that the type has a close and has an __exit__, we may also need to manually specify the supported types for the lint since not all __exit__s will call close

sbdchd avatar Jun 13 '21 23:06 sbdchd