Skip to content

Imports in multiline try blocks are not properly ignored when determining the necessary packages for a modeling file #23667

Description

@dakinggg

System Info

  • transformers version: 4.29.2
  • Platform: macOS-13.3.1-arm64-arm-64bit
  • Python version: 3.10.11
  • Huggingface_hub version: 0.14.1
  • Safetensors version: not installed
  • PyTorch version (GPU?): 2.0.1 (False)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: N/A
  • Using distributed or parallel set-up in script?: N/A

Who can help?

@sgugger

Reproduction

The minimal repro is to just call https://github.com/huggingface/transformers/blob/v4.29.2/src/transformers/dynamic_module_utils.py#L118-L134 on a file that has a multiline try import, e.g.

try:
    from package import function
    from package2 import function
except:
    pass

and run the get_imports function on it. The output will be ['package', 'package2'], when it should be []

Expected behavior

Imports in multiline try blocks should be ignored when determining what packages a modeling file requires. I believe

content = re.sub(r"\s*try\s*:\s*.*?\s*except\s*:", "", content, flags=re.MULTILINE)
just needs to be modified to include flags=re.MULTILINE | re.DOTALL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions