pyinat / pyinaturalist

Python client for iNaturalist
https://pyinaturalist.readthedocs.io
MIT License
130 stars 17 forks source link

Using Dry Run throws a key error #536

Closed arky closed 8 months ago

arky commented 8 months ago

The problem

A description of what the bug is, including a complete traceback (if applicable)

Expected behavior

Setting 'dry_run' to true results in a key error. However it would run correctly without it.

Steps to reproduce the behavior


 enable_logging()

    create_observation(
        taxon_id=40268, 
        observed_on_string=time,
        time_zone="Bangkok",
        latitude="10.6122382",
        longitude="104.1774368",
        description=" some description",
        access_token=token,
        dry_run=True
        )
[01-09 15:48:17] INFO     Request: (DRY RUN)                                                         [session.py](file:///usr/local/lib/python3.10/dist-packages/pyinaturalist/session.py):[297](file:///usr/local/lib/python3.10/dist-packages/pyinaturalist/session.py#297)
                          POST https://api.inaturalist.org/v1/observations?ignore_photos=1                         
                          User-Agent: python-requests/2.31.0 pyinaturalist/0.19.0                                  
                          Accept-Encoding: gzip, deflate, br                                                       
                          Accept: application/json                                                                 
                          Connection: keep-alive                                                                   
                          Authorization: [REDACTED]                                                                
                          Content-Length: 201                                                                      
                          Content-Type: application/json                                                           
                          {'observation': {'taxon_id': 40268, 'observed_on_string':                                
                          '2023-12-17T18:27:29+07:00', 'time_zone': 'Bangkok', 'description': ' some               
                          description', 'latitude': '10.6122382', 'longitude': '104.1774368'}}                     

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[17], line 17
     14 print(time)
     15 enable_logging()
---> 17 create_observation(
     18     taxon_id=40268, 
     19     observed_on_string=time,
     20     time_zone="Bangkok",
     21     latitude="10.6122382",
     22     longitude="104.1774368",
     23     description=" some description",
     24     access_token=token,
     25     dry_run=True
     26     )

File /usr/local/lib/python3.10/dist-packages/forge/_revision.py:328, in Revision.__call__.<locals>.inner(*args, **kwargs)
    324 @functools.wraps(callable)  # type: ignore
    325 def inner(*args, **kwargs):
    326     # pylint: disable=E1102, not-callable
    327     mapped = inner.__mapper__(*args, **kwargs)
--> 328     return callable(*mapped.args, **mapped.kwargs)

File /usr/local/lib/python3.10/dist-packages/pyinaturalist/v1/observations.py:418, in create_observation(**params)
    416 response = post(f'{API_V1}/observations', json={'observation': params}, **kwargs)
    417 response_json = response.json()
--> 418 observation_id = response_json['id']
    420 upload(observation_id, photos=photos, sounds=sounds, photo_ids=photo_ids, **kwargs)
    421 return response_json

KeyError: 'id'

Workarounds

Is there an existing workaround for this issue?

Environment

JWCook commented 8 months ago

Thanks for the bug report. This has been fixed in main.

arky commented 7 months ago

Thank you @JWCook Much appreciated.