Skip to content

Consider failing on warnings (and ResourceWarning)

Non ResourceWarning: warnings

During our test execution there might be Warnings that might be ignored. Passing -W error to Python would make the command fail.

#!/usr/bin/env python3

import warnings

def generate_deprecation_warning():
    warnings.warn('deprecated', DeprecationWarning)

generate_deprecation_warning()

Compare:

  • python3 ./a.py ; echo $?
  • python3 -W error ./a.py ; echo $?
  • PYTHONWARNINGS=error ./a.py ; echo $? (same as before but can be done for all the python3 processes in CI)

ResourceWarning: warnings

ResourceWarnings are usually only printed but they don't raise an exception (when they happen in del methods).


#!/usr/bin/env python3

import warnings
import aiohttp
import asyncio

async def generate_resource_warning():
    # This will generate a ResourceWarning: if not calling
    # await c.close() before it gets destroyed
    c = aiohttp.ClientSession()

asyncio.run(generate_resource_warning())

Because they don't raise an exception the -W error doesn't stop the execution.

An idea is to wrap the python3 execution with something to detect and fail if ResourceWarning: appears in the stderr (or anything in stderr?).

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information