davidhalter / davidhalter/jedi

Unhandled Exception with Unnamed Lambdas When Analyzing References Using dynaconf

Open
#1,978 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
6.2k
Forks
535
PR merge metrics
No merged PRs in 30d

Description

## Issue Description

Hi, I've encountered an issue with `jedi` where it fails to process files that **doesn't** contain lambda expressions. This issue arose after I introduced `dynaconf` to manage the project configurations, which might be altering the import mechanism in a way `jedi` can't handle.

## Steps to Reproduce
1. Introduce `dynaconf` for project configuration management.
2. Use `jedi` to analyze the codebase for references.
3. Encounter errors related to unnamed lambda expressions.

Here is dynaconf code of `setting.py`

```python
from dynaconf import Dynaconf

settings = Dynaconf(
environments=True,
env="production",
settings_files="repo_agent/configs/settings.toml",
envvar_prefix="REPOAGENT"
)
```

The file `file_handler.py` needs to analyse.
```python
import git
import os,json
import ast
from setting import settings as CONFIG
from utils.gitignore_checker import GitignoreChecker

class FileHandler:
def __init__(self, repo_path, file_path):
self.file_path = file_path
self.repo_path = repo_path
self.project_hierarchy = os.path.join(repo_path, CONFIG['project_hierarchy'])

def convert_all_to_markdown_files_from_json(self):
"""
Converts all files to markdown format based on the JSON data.

Reads the project hierarchy from a JSON file, checks if the Markdown_docs folder exists,
creates it if it doesn't, and then iterates through each file in the JSON data.
For each file, it converts the file to markdown format and writes it to the Markdown_docs folder.

Args:
self (object): The file_handler object.

Returns:
None
"""
with open(self.project_hierarchy, 'r', encoding='utf-8') as f:
json_data = json.load(f)

markdown_docs_path = os.path.join(self.repo_path, CONFIG['Markdown_Docs_folder'])
if not os.path.exists(markdown_docs_path):
os.mkdir(markdown_docs_path)

for rel_file_path, file_dict in json_data.items():
md_path = os.path.join(markdown_docs_path, rel_file_path.replace('.py', '.md'))
markdown = self.convert_to_markdown_file(rel_file_path)

os.makedirs(os.path.dirname(md_path), exist_ok=True)

with open(md_path, 'w', encoding='utf-8') as f:
f.write(markdown)
```

`Jedi` Usage
```python
import jedi

script = jedi.Script(path='/workspaces/RepoAgent/repo_agent/file_handler.py')
references = script.get_references(line=16, column=8)
print(references)
```

I use `from config import CONFIG` previously, which is `config.py`
```python
import yaml

CONFIG = yaml.load(open('config.yml', 'r'), Loader=yaml.FullLoader)
```

I am puzzled by this matter. From `from config import CONFIG` to `from setting import settings as CONFIG`, and this `import` sentence have been used in other file which works fine.

## Actual Behavior
When running `jedi` to get references in my project, I receive the following error messages for files that contain lambda expressions:

```python
File "/home/codespace/.local/lib/python3.10/site-packages/parso/python/tree.py", line 669, in name
raise AttributeError("lambda is not named.")
AttributeError: lambda is not named.
```

## Environment Information
- Jedi version: `0.19.1`
- Python version: Python 3.10.13
- Operating System: Linux

I have attached tracert logs showing the error messages.

[tracert.txt](https://github.com/davidhalter/jedi/files/13862653/tracert.txt)

Thank you for looking into this issue. Please let me know if there's any additional information I can provide.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.