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

Used submodule import flagged as unused in except handler #11448

Open
bnomis opened this issue May 16, 2024 · 2 comments
Open

Used submodule import flagged as unused in except handler #11448

bnomis opened this issue May 16, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@bnomis
Copy link

bnomis commented May 16, 2024

Ruff version 0.4.4

Flags import some.module in the below as unused F401 but it is used

https://play.ruff.rs/f84ea9b2-0401-491f-a6a9-20f4e370d47f

import logging

import some.module  # ruff thinks this is not used


def func():
    logger = logging.getLogger('some')
    try:
        import some.other
        print(some.other.version)
        return None
    except Exception as e:
        logger.exception('exception' % e, extra={'now': some.module.now()})
        raise

If I move the import into the function - it's no longer flagged:

import logging


def func():
    import some.module  # now it's not flagged

    logger = logging.getLogger('some')
    try:
        import some.other
        print(some.other.version)
        return None
    except Exception as e:
        logger.exception('exception' % e, extra={'now': some.module.now()})
        raise
@charliermarsh charliermarsh added the bug Something isn't working label May 17, 2024
@charliermarsh
Copy link
Member

This seems like a mix of some issues with modeling submodule imports (we roughly treat import some.module and import some.other as equivalent) and control flow analysis.

There's more on submodule imports in (e.g.) #4656.

@charliermarsh
Copy link
Member

The interesting thing about submodule imports is -- this on its own will often:

def func():
    logger = logging.getLogger('some')
    try:
        import some.other
        print(some.other.version)
        return None
    except Exception as e:
        logger.exception('exception' % e, extra={'now': some.module.now()})
        raise

@charliermarsh charliermarsh changed the title Used import flagged as unused F401 Used submodule import flagged as unused in except handler May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants