pyresample icon indicating copy to clipboard operation
pyresample copied to clipboard

Unhelpful error message from parse_area_file if file not found

Open gerritholl opened this issue 4 years ago • 2 comments

Code Sample, a minimal, complete, and verifiable piece of code

from pyresample.area_config import parse_area_file
parse_area_file("filenotfound")

Problem description

This fails with an AttributeError. This wasted me some time as I thought that the problem was with the contents of the yaml-file or elsewhere.

Expected Output

I'd like to see this raise a FileNotFoundError.

Actual Result, Traceback if applicable

Traceback (most recent call last):
  File "mwe6.py", line 2, in <module>
    parse_area_file("filenotfound")
  File "/media/nas/x21324/miniconda3/envs/py37e/lib/python3.7/site-packages/pyresample/area_config.py", line 104, in parse_area_file
    return _parse_yaml_area_file(area_file_name, *regions)
  File "/media/nas/x21324/miniconda3/envs/py37e/lib/python3.7/site-packages/pyresample/area_config.py", line 139, in _parse_yaml_area_file
    area_dict = _read_yaml_area_file_content(area_file_name)
  File "/media/nas/x21324/miniconda3/envs/py37e/lib/python3.7/site-packages/pyresample/area_config.py", line 123, in _read_yaml_area_file_content
    area_dict = recursive_dict_update(area_dict, tmp_dict)
  File "/media/nas/x21324/miniconda3/envs/py37e/lib/python3.7/site-packages/pyresample/utils/__init__.py", line 231, in recursive_dict_update
    for k, v in u.items():
AttributeError: 'str' object has no attribute 'items'

Versions of Python, package at hand and relevant dependencies

Python 3.7.3, pyresample 1.13.2.

gerritholl avatar Dec 03 '19 16:12 gerritholl

So a little explanation on this, the filenames don't have to be just filenames, they can also be file objects or regular strings of YAML formatted text. The error message you're seeing is what happens when it tried to parse it as YAML. Since a pure string "filenotfound" is valid YAML it didn't fail to parse it and assumed it was a dictionary and kept on processing/analyzing. Pyresample can only falls back to YAML parsing if the string provided is not found as a file on disk.

I think the best solution might be to check that the parsed YAML is a dictionary and/or if the YAML didn't parse and then provide an error message that either the file was not found or it is invalid YAML.

djhoese avatar Dec 03 '19 18:12 djhoese

Pull requests welcome! :wink:

djhoese avatar Dec 03 '19 18:12 djhoese