8000 Assignment in assert statement cause unbounded variable error in python -O · Issue #133 · ecmwf/cdsapi · GitHub
[go: up one dir, main page]

Skip to content

Assignment in assert statement cause unbounded variable error in python -O #133

Open

Description

@juanqiu1

What happened?

In cdsapi 0.7.4, its dependency cads-api-client has a faulty statement in file process.py:204. It writes as

    @property
    def _json_dict(self) -> dict[str, Any]:
        assert isinstance(content := self.json, dict)
        return content

However, if -O mode is invoked by Python, optimization will not execute assert line, making variable content undeclared, hence return statement will raise error.

What are the steps to reproduce the bug?

execute script using cdsapi with python -O

Version

0.7.2-0.7.4

Platform (OS and architecture)

OS Independent

Relevant log output

[vscode@d9e105258f04 daily-era5-downloader]$ python -O handler.py 
Downloading files for the following times: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
/opt/conda/lib/python3.12/site-packages/cads_api_client/api_client.py:83: UserWarning: cannot access local variable 'content' where it is not associated with a value
  warnings.warn(str(exc), UserWarning)
Traceback (most recent call last):
  ....... <intentionally hide>.....
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/legacy_api_client.py", line 169, in retrieve
    submitted = self.client.submit_and_wait_on_results(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/api_client.py", line 458, in submit_and_wait_on_results
    return self._retrieve_api.submit(collection_id, **request).make_results()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/processing.py", line 743, in submit
    return self.get_process(collection_id).submit(**request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/processing.py", line 732, in get_process
    return Process.from_request("get", url, **self._request_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/processing.py", line 191, in from_request
    self.log_messages()
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/processing.py", line 215, in log_messages
    if message_str := self._json_dict.get("message"):
                      ^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.12/site-packages/cads_api_client/processing.py", line 207, in _json_dict
    return content
           ^^^^^^^
UnboundLocalError: cannot access local variable 'content' where it is not associated with a value

Accompanying data

No response

Organisation

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0