davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.78k stars 508 forks source link

Unhandled Exception with Unnamed Lambdas When Analyzing References Using dynaconf #1978

Open Umpire2018 opened 9 months ago

Umpire2018 commented 9 months ago

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

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.

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

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

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:

  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

I have attached tracert logs showing the error messages.

tracert.txt

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