Awesome autocompletion, static analysis and refactoring library for python

Overview

Jedi - an awesome autocompletion, static analysis and refactoring library for Python

The percentage of open issues and pull requests The resolution time is the median time an issue or pull request stays open. Tests PyPI Downloads

Jedi is a static analysis tool for Python that is typically used in IDEs/editors plugins. Jedi has a focus on autocompletion and goto functionality. Other features include refactoring, code search and finding references.

Jedi has a simple API to work with. There is a reference implementation as a VIM-Plugin. Autocompletion in your REPL is also possible, IPython uses it natively and for the CPython REPL you can install it. Jedi is well tested and bugs should be rare.

Jedi can currently be used with the following editors/projects:

and many more!

There are a few language servers that use Jedi:

Here are some pictures taken from jedi-vim:

https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_complete.png

Completion for almost anything:

https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_function.png

Documentation:

https://github.com/davidhalter/jedi/raw/master/docs/_screenshots/screenshot_pydoc.png

Get the latest version from github (master branch should always be kind of stable/working).

Docs are available at https://jedi.readthedocs.org/en/latest/. Pull requests with enhancements and/or fixes are awesome and most welcome. Jedi uses semantic versioning.

If you want to stay up-to-date with releases, please subscribe to this mailing list: https://groups.google.com/g/jedi-announce. To subscribe you can simply send an empty email to [email protected].

Issues & Questions

You can file issues and questions in the issue tracker <https://github.com/davidhalter/jedi/>. Alternatively you can also ask on Stack Overflow with the label python-jedi.

Installation

Check out the docs.

Features and Limitations

Jedi's features are listed here: Features.

You can run Jedi on Python 3.6+ but it should also understand code that is older than those versions. Additionally you should be able to use Virtualenvs very well.

Tips on how to use Jedi efficiently can be found here.

API

You can find a comprehensive documentation for the API here.

Autocompletion / Goto / Documentation

There are the following commands:

  • jedi.Script.goto
  • jedi.Script.infer
  • jedi.Script.help
  • jedi.Script.complete
  • jedi.Script.get_references
  • jedi.Script.get_signatures
  • jedi.Script.get_context

The returned objects are very powerful and are really all you might need.

Autocompletion in your REPL (IPython, etc.)

Jedi is a dependency of IPython. Autocompletion in IPython with Jedi is therefore possible without additional configuration.

Here is an example video how REPL completion can look like. For the python shell you can enable tab completion in a REPL.

Static Analysis

For a lot of forms of static analysis, you can try to use jedi.Script(...).get_names. It will return a list of names that you can then filter and work with. There is also a way to list the syntax errors in a file: jedi.Script.get_syntax_errors.

Refactoring

Jedi supports the following refactorings:

  • jedi.Script.inline
  • jedi.Script.rename
  • jedi.Script.extract_function
  • jedi.Script.extract_variable

Code Search

There is support for module search with jedi.Script.search, and project search for jedi.Project.search. The way to search is either by providing a name like foo or by using dotted syntax like foo.bar. Additionally you can provide the API type like class foo.bar.Bar. There are also the functions jedi.Script.complete_search and jedi.Project.complete_search.

Development

There's a pretty good and extensive development documentation.

Testing

The test suite uses pytest:

pip install pytest

If you want to test only a specific Python version (e.g. Python 3.8), it is as easy as:

python3.8 -m pytest

For more detailed information visit the testing documentation.

Acknowledgements

Thanks a lot to all the contributors!

Comments
  • What are your most typical mistakes that you would like a static analysis tool pick up?

    What are your most typical mistakes that you would like a static analysis tool pick up?

    I'm currently creating a static analysis/linter tool within Jedi.

    I have realized that we can detect quite a lot of errors in Python code. Which are the mistakes that the static analysis tools (pylint, pyflakes, etc) usually don't detect? What are the most typical errors that you have to fix after running your tests?

    Jedi already detects quite a few AttributeErrors, for example: https://github.com/davidhalter/jedi/blob/linter/test/static_analysis/attribute_error.py

    I don't need deep research, just tell me what you would like to be detected @tkf, @dbrgn, @asmeurer, @ColinDuquesnoy and everybody else.

    discussion 
    opened by davidhalter 87
  • Request: Instructions for Installing Hooks for Large Packages

    Request: Instructions for Installing Hooks for Large Packages

    First, thanks very much for jedi, it is a fantastic library. I just went through the process of adding jedi to the Spyder IDE in place of rope (for completions, calltips, docstrings, and goto), and it went very smoothly.
    However, the one thing holding me back is the >18sec it takes to complete on numpy on my machine, every time I start the editor, since scientific computing is the target use for Spyder. You mentioned it was possible to install "hooks" for large dynamic libraries, but I could not figure out how based on poking through the code base. Can you offer any more insight?

    release-discussion 
    opened by blink1073 71
  • Get rid of multi-dimensional array

    Get rid of multi-dimensional array

    parsing_representation.Array is now multidimensional, but should be one dimensional (containing parsing_representation.Statement only.

    This improves both the size of the parser and readability. But it affects almost everything.

    (still some work to be done.)

    opened by davidhalter 67
  • goto() import following

    goto() import following

    I have been using get_definitions() to implement what is now the goto() function. This worked great until now. I tried using goto(), but there are these two issues. When goto() is invoked on a name in an import statement that absolutely imports from within the current package, nothing is found. In my case it looks like this: from myproject.mod import SomeClass with the cursor on SomeClass. This used to work with get_definitions() and accessing scope.parent. myproject is in sys.path.

    Second, I would like to jump to the definition directly, without stopover at the import statement.

    Are you planning to enhance goto() like this or do I need to implement this myself?

    Anyway, great work!

    opened by tek 59
  • Logo / Mascot

    Logo / Mascot

    I think this would be a great "logo / mascot" for Jedi (to be used in the docs / readme etc...):

    Source: http://alanrobinson.deviantart.com/art/yoda-sketch-29439547

    We'd have to ask for permission to use it first of course :)

    discussion 
    opened by dbrgn 55
  • Refactoring with the new parser

    Refactoring with the new parser

    Note: #103 Discussed a lot of things about introducing refactoring. Now that we're a step further, let's discuss the API. This writeup is the result of someone wanting to contribute towards refactoring.

    Refactoring

    It's been a while, but since I've been ask how to tackle the issue, let's start simple:

    1. Use the latest branch (currently linter)
    2. Small of initial refactorings.
    3. Specify the API is unfortunately the first thing we need to do. Very happy for feedback there.
    4. Testing

    The first and easiest refactorings that I'd like to see are:

    • Renaming
    • Extract Variable
    • Inline Variable

    One of the next steps could be (or basically anything else):

    • Extract Function/Class
    • Inline Function/Class

    API

    I'm suggesting the following API (note that Script is given a pointer (line / column index) as well as the path of the file::

    def rename(script: jedi.Script, new_name: str) -> Refactoring:
    
    def extract(script: jedi.Script, new_name: str, end: Tuple[int, int]): -> Refactoring
        """
        The starting position is the line/column input of jedi.Script and the
        `end` position the last place, that you want to exclude in your
        exctract logic.
        """
    
    # Doesn't need anything additional, since it just deletes:
    def inline(script: jedi.Script -> Refactoring):
    

    Of course the bigger work is defining the return class::

    class Refactoring():
        def __init__(self, whatever_we_need_its_private):
            pass
    
        def get_changed_files(self):
    
        def get_renames(self) -> List[Tuple[str, str]]:
            """
            Files can be renamed in a refactoring.
            """
    
        def diff(self) -> str:
    
    class RefactoringError(Exception):
        def __init__(self, message: str):
    

    Please critize it! I'm very happy for all the feedback, especially if you don't like it. Annotations will of course be removed (and changed to docstrings), because we have to maintain Python 2/3 compatibility.

    Testing

    Since I have previously started a bit of work on refactoring, I think it would be very beneficial to use that test suite. There's a small list of tests already in test/refactor/* (renaming, extract/inline variable). I'm very happy to give pointers about how to use them. Currently they are disabled. Just enable the switch again in test/test_integration.py.

    Issues

    Currently there's only one known issue (will add to this, if there's more): The parser does error recovery. For now just ignore this, but when we're pushing this to the users, we need to take care of it.

    Hints

    If you are implementing all of this, you have to use the parser tree's jedi.parser.tree logic. If you think that doing something requires a lot of code, you're probably wrong (or the code should just be part of the tree).

    Generally a refactoring has 3 steps:

    • Get type inference information, so you know what to refactor, what to remove or change (this is part of Jedi's API and should probably use almost no work). E.g. rename should use jedi.Script.usages; extract should use no type inference and inline should work with jedi.Script.goto_definitions. There's always reasons why
    • Modify the parser accordingly. Abuse Leaf.value and Leaf.prefix and move nodes around (shouldn't cause any issues).
    • Call Parser.module.get_code() and profit $$$ !

    The module jedi/refactor.py has a very similar class structure as the one proposed above, but it doesn't work. You can throw away basically anything. It can give you an idea how the structure would look like, but the code is crap. THROW IT AWAY.

    Please let me know if you need any help to start your work.

    feature 
    opened by davidhalter 51
  • GTK+ 3 (GObject introspection)

    GTK+ 3 (GObject introspection)

    It appears the jedi can't handle the GObject introspection module (using jedi 0.8.0-final0 on Ubuntu 14.04):

    s3 = jedi.Script('from gi.repository import Gtk\nGtk.')
    s3.completions()
    
    s2 = jedi.Script('import gtk\ngtk.')
    s2.completions()
    

    s2 completions show as expected, s3 is an empty list. Note that if I manually import Gtk3 and type dir(Gtk) I get a complete list of module symbols.

    Are there any workarounds for this? (Don't get me started on what a disaster GI is for long suffering GTK+ app devs.)

    feature low-prio 
    opened by spillz 50
  • Missing Doc Strings for Classes and Modules

    Missing Doc Strings for Classes and Modules

    Using v0.7.0

    Maybe I'm missing something obvious, but

    import jedi
    s = jedi.Script('import jedi\njed',2,3)
    c = s.completions()[0]
    print c
    print c.doc == ''
    print jedi.__doc__ == ''
    
    s = jedi.Script('import jedi\njedi.Scr',2,8)
    c = s.completions()[0]
    print c
    print c.doc == ''
    print jedi.Script.__doc__ == ''
    

    returns

    <Completion: jedi>
    True
    False
    <Completion: Script>
    True
    False
    

    Is there some way to enable these doc strings?

    I want to be able to display doc popups next to completion popup items as per the screenshot below (which currently only works with functions because of this limitation):

    codeblocks-jedi-popup

    bug 
    opened by spillz 46
  • Create a unified tech stack for jedi, rope, RedBaron, yapf, pylint, pep8, and similar tools and libraries

    Create a unified tech stack for jedi, rope, RedBaron, yapf, pylint, pep8, and similar tools and libraries

    Currently there exists duplication of effort around tooling for parsing, automated manipulation, and analyzing Python code. Each project in this space faces similar fundamental problems of requiring a data representation of the Python code of interest, most commonly represented as a syntax tree. Here is a list, by no means exhaustive, of tools and libraries that face the problem of needing a representation of Python code capable of inspection and possibly even manipulation:

    The overlap between any of these projects is by no means complete, but the overlap between all of them is still significant, especially given they solve the same underlying problem of Python code representation. My hope here is we can examine whether the Python ecosystem really needs this many implementations of the Python syntax tree, and whether we can arrive at an ecosystem that has a unified base, with the diversity of effort occurring on more interesting problem spaces such as advanced refactoring and static analyses.

    I am hoping to start the discussion here in jedi's GitHub Issues because of the project's recent announcement of a representation of Python code based upon an (improved?) fork of lib2to3, which, from my understanding should provide a usable syntax tree representation for both Python 2 and Python 3.

    One question is whether the jedi library's API is wide enough to support projects beyond jedi, and, if it's not, what additions must be made to make it more universal? We must acknowledge that each tool and library mentioned in the list above may only need some portion of information from the syntax tree, but that a good underlying representation would have the union of such information required by all client tools and libraries. Clearly some requirements gathering must be done, but my hope is there is a strong and reconcilable overlap in requirements between the projects.

    Another question is, should this library be broken out into its own Python distribution package? This would allow the fundamental underlying library to move at its own pace independent of jedi's release schedule. This means jedi would become a client of library, as I hope other libraries and tools could.

    It also seems that the base representation, in being an accurate representation of Python code, may not be the most convenient or appropriate interface through which a client should interact with the code, suggesting room for higher level APIs, provided by separate libraries that wrap the base representation, for example, how pandas is to numpy. I think there exists the opportunity to create a library stack, in which the tools mentioned above become clients of the high-level libraries mentioned above, which in turn are clients of the base representation library. In fact, we can see discussion of these very ideas already within and between multiple projects, e.g., python-rope/rope#57, and Psycojoker/baron#61. I think now is the time to get serious about these efforts.

    Finally, I'd like to point to two inspirations that this collaboration of effort and unification can happen. The first inspiration is the scientific Python community's unification around the HDF5 Python stack, which I encourage you to read about in @scopatz's excellent blog post about that effort. The second inspiration comes from @royrapoport's description of the way Netflix works in his recent interview on Talk Python To Me. There, teams are permitted to make their own engineering decisions, but this inevitably creates duplication of effort. To counteract that duplication, Netflix periodically evaluates overlapping projects, and if their duplication is found technically unjustifiable, the project's maintainers must sit down with each other and figure out how to merge the code bases. While there's no fiduciary incentive in the case of the projects I've mentioned above here, there is still the incentive of reducing the amount of time, personally and collectively, spent solving very similar problems.

    I think a similar effort here to unify would be a great win for Python tooling and the Python community. My hope is this opens up the conversation between these various projects.

    cc @davidhalter @aligrudi @mcepl @Psycojoker @gwelymernans @hayd @jcrocholl @florentx @IanLee1521 @PCManticore

    I will also try to reach out to maintainers of other projects whom I could not find on GitHub, but if anyone here has contacts mentioned or omitted but interested, please reach out to them as well.

    @davidhalter, forgive me for hijacking your project's Issues for this.

    Apologies also for anyone who is offended this discussion would happen on GitHub or in a GitHub issue. To me this seems the most open, publicly visible, conducive space to begin the conversation, though, as I suspect few people are members of the Python code-quality mailing list when compared to the number of people impacted and interested.

    discussion 
    opened by gotgenes 45
  • Jedi API for Python REPLs

    Jedi API for Python REPLs

    I just noticed that I can create fake scopes from "raw" Python objects. This is just a quick first version of it. Currently it just treats objects with __module__ attribute. I need to treat other cases such as instance and module. I thought I'd post it first before I am going to wrong direction.

    Here is a working example:

        >>> from itertools import chain
        >>> script = Interpreter('chain().f', [locals()])
        >>> script.complete()
        [<Completion: <InstanceElement of from_iterable>>]
    
    next_release 
    opened by tkf 43
  • Normal memory usage?

    Normal memory usage?

    I have a long-running python jedi process controlled by Code::Blocks IDE. I have noticed that when working with big libraries like numpy or pandas that each time I retrieve a completion hint or call tip on one of those modules anywhere between 0 and 2MB of memory is swallowed and apparently never freed. I can easily have a process running jedi on only those libs that consumes a couple of hundred MBs.

    1. Is that expected memory usage?
    2. Is there any way to free some of the memory being used short of killing the process without also hurting performance?

    PS: I should caveat that it's possible that jedi isn't the offender here, but something in my implementation, but thought I would check to see what's considered normal.

    discussion 
    opened by spillz 42
  • Question/Feature request: Can I disable adding `name=` during autocompletion of function arguments?

    Question/Feature request: Can I disable adding `name=` during autocompletion of function arguments?

    If not, how does Jedi decide when it is valid to add them? Can I change something in my typing maybe? C functions in python do not allow for named arguments.

    opened by jpfeuffer 1
  • iPython Jedi completion result timing depends sensitively on CWD

    iPython Jedi completion result timing depends sensitively on CWD

    At ipython/ipython#13866 I reported an issue in which completions inside an astropy.units object (x.unit.[Tab]) produce results in widely varying amounts of time, depending on the current working directory. If iPython is in a deep directory (like ~/) Jedi actually crashes. Not-so-deep but significant (~/code) takes 30 or 40s, and a shallow directory at the end of the tree structure completes in about a second, with the same results.

    So it seems a static analysis of the entire directory tree, starting at iPython's current working directory, is occurring. It's not clear why. Reproduction recipe in the above. See also #1446.

    opened by jdtsmith 0
  • Get reference in type annotation

    Get reference in type annotation

    Jedi seems to not pick up reference that are in type annotation or in f-string. This creates issue when using rename has to reference are not changed.

    This snippet prints only one reference when it should find three.

    import jedi
    source = '''
    class Foo:
        def return_me(self) -> "Foo":
            return
    
        def __str__(self) -> str:
            return "foo"
    '''
    s = f"My class {Foo()}"
    
    script=jedi.Script(code=source)
    for ref in script.get_references(line=2, column=7):
        print(ref)
    

    Output:

    <Name full_name='__main__.Foo', description='class Foo'>
    

    I don't know if that is conscious choice, but it would be able to pick those references. I'd be happy to help, maybe by coding a failing test first, if someone can point me where it should go.

    opened by enadeau 3
  • 0.18.2: pytest is failing in one unit

    0.18.2: pytest is failing in one unit

    I'm packaging your module as an rpm package so I'm using the typical PEP517 based build, install and test cycle used on building packages from non-root account.

    • python3 -sBm build -w --no-isolation
    • because I'm calling build with --no-isolation I'm using during all processes only locally installed modules
    • install .whl file in </install/prefix>
    • run pytest with PYTHONPATH pointing to sitearch and sitelib inside </install/prefix>

    Here is pytest output:

    + PYTHONPATH=/home/tkloczko/rpmbuild/BUILDROOT/python-jedi-0.18.2-2.fc35.x86_64/usr/lib64/python3.8/site-packages:/home/tkloczko/rpmbuild/BUILDROOT/python-jedi-0.18.2-2.fc35.x86_64/usr/lib/python3.8/site-packages
    + /usr/bin/pytest -ra
    =========================================================================== test session starts ============================================================================
    platform linux -- Python 3.8.15, pytest-7.2.0, pluggy-1.0.0
    rootdir: /home/tkloczko/rpmbuild/BUILD/jedi-0.18.2, configfile: pytest.ini, testpaths: jedi, test
    collected 3863 items
    
    jedi/__init__.py .                                                                                                                                                   [  0%]
    jedi/api/__init__.py .                                                                                                                                               [  0%]
    jedi/api/classes.py .....                                                                                                                                            [  0%]
    jedi/api/replstartup.py .                                                                                                                                            [  0%]
    jedi/inference/context.py .                                                                                                                                          [  0%]
    jedi/inference/docstrings.py ..                                                                                                                                      [  0%]
    jedi/inference/sys_path.py .                                                                                                                                         [  0%]
    test/conftest.py .                                                                                                                                                   [  0%]
    test/test_cache.py ..                                                                                                                                                [  0%]
    test/test_debug.py .                                                                                                                                                 [  0%]
    test/test_file_io.py ..                                                                                                                                              [  0%]
    test/test_integration.py ........................................................................................................................................... [  4%]
    .................................................................................................................................................................... [  8%]
    .................................................................................................................................................................... [ 12%]
    ................................................................ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss.............. [ 16%]
    .................................................................................................................................................................... [ 21%]
    .................................................................................................................................................................... [ 25%]
    .................................................................................................................................................................... [ 29%]
    .................................................................................................................................................................... [ 33%]
    .................................................................................................................................................................... [ 38%]
    .................................................................................................................................................................... [ 42%]
    .................................................................................................................................................................... [ 46%]
    .................................................................................................................................................................... [ 50%]
    ...................................................^Z
    [1]+  Stopped                 rpmbuild -ba --with check --with failing_tests python-jedi.spec
    
    [tkloczko@pers-jacek SPECS]$ fg
    rpmbuild -ba --with check --with failing_tests python-jedi.spec
    ................................................................................................................. [ 55%]
    .................................................................................................................................................................... [ 59%]
    ................................                                                                                                                                     [ 60%]
    test/test_settings.py ...                                                                                                                                            [ 60%]
    test/test_utils.py ..........                                                                                                                                        [ 60%]
    test/test_api/test_analysis.py .                                                                                                                                     [ 60%]
    test/test_api/test_api.py ..................................................                                                                                         [ 61%]
    test/test_api/test_api_classes_follow_definition.py ....                                                                                                             [ 61%]
    test/test_api/test_call_signatures.py .............................................................................................................................. [ 65%]
    ...............................................................................                                                                                      [ 67%]
    test/test_api/test_classes.py ............................................                                                                                           [ 68%]
    test/test_api/test_documentation.py ..                                                                                                                               [ 68%]
    test/test_api/test_classes.py ......                                                                                                                                 [ 68%]
    test/test_api/test_documentation.py ..                                                                                                                               [ 68%]
    test/test_api/test_classes.py ......                                                                                                                                 [ 68%]
    test/test_api/test_documentation.py ..                                                                                                                               [ 68%]
    test/test_api/test_classes.py .........................................................                                                                              [ 70%]
    test/test_api/test_completion.py ................................................................................................................................... [ 73%]
    ............                                                                                                                                                         [ 73%]
    test/test_api/test_context.py .........................................                                                                                              [ 74%]
    test/test_api/test_documentation.py .................................                                                                                                [ 75%]
    test/test_api/test_environment.py ..ss.s..............                                                                                                               [ 76%]
    test/test_api/test_full_name.py .............                                                                                                                        [ 76%]
    test/test_api/test_interpreter.py .................................................................................................................................. [ 80%]
    ...                                                                                                                                                                  [ 80%]
    test/test_api/test_keyword.py ....                                                                                                                                   [ 80%]
    test/test_api/test_names.py ........................                                                                                                                 [ 80%]
    test/test_api/test_project.py .........................................................                                                                              [ 82%]
    test/test_api/test_refactoring.py ....                                                                                                                               [ 82%]
    test/test_api/test_search.py ............................                                                                                                            [ 83%]
    test/test_api/test_settings.py .                                                                                                                                     [ 83%]
    test/test_api/test_signatures.py ...................                                                                                                                 [ 83%]
    test/test_api/test_syntax_errors.py .........                                                                                                                        [ 83%]
    test/test_api/test_unicode.py ......                                                                                                                                 [ 84%]
    test/test_api/test_usages.py ............                                                                                                                            [ 84%]
    test/test_inference/test_annotations.py .......                                                                                                                      [ 84%]
    test/test_inference/test_buildout_detection.py ......                                                                                                                [ 84%]
    test/test_inference/test_compiled.py ...........................                                                                                                     [ 85%]
    test/test_inference/test_context.py ..                                                                                                                               [ 85%]
    test/test_inference/test_docstring.py .............sssssssssssss..................                                                                                   [ 86%]
    test/test_inference/test_extension.py .....                                                                                                                          [ 86%]
    test/test_inference/test_fstring.py .                                                                                                                                [ 86%]
    test/test_inference/test_implicit_namespace_package.py ......                                                                                                        [ 86%]
    test/test_inference/test_imports.py ..........................xxxx...........................................                                                        [ 88%]
    test/test_inference/test_literals.py ...                                                                                                                             [ 88%]
    test/test_inference/test_mixed.py .................                                                                                                                  [ 89%]
    test/test_inference/test_namespace_package.py ................                                                                                                       [ 89%]
    test/test_inference/test_precedence.py ..x                                                                                                                           [ 89%]
    test/test_inference/test_pyc.py ..                                                                                                                                   [ 89%]
    test/test_inference/test_representation.py ..                                                                                                                        [ 89%]
    test/test_inference/test_signature.py ..................................................................................................                             [ 92%]
    test/test_inference/test_stdlib.py .......                                                                                                                           [ 92%]
    test/test_inference/test_sys_path.py .F................ss.....                                                                                                       [ 93%]
    test/test_inference/test_gradual/test_conversion.py ......                                                                                                           [ 93%]
    test/test_inference/test_gradual/test_stub_loading.py ...........................                                                                                    [ 94%]
    test/test_inference/test_gradual/test_stubs.py ..................................................................................................................... [ 97%]
    ...............................................................                                                                                                      [ 98%]
    test/test_inference/test_gradual/test_typeshed.py ............................                                                                                       [ 99%]
    test/test_parso_integration/test_basic.py ......                                                                                                                     [ 99%]
    test/test_parso_integration/test_error_correction.py ....                                                                                                            [ 99%]
    test/test_parso_integration/test_parser_utils.py ........                                                                                                            [100%]
    
    ================================================================================= FAILURES =================================================================================
    ____________________________________________________________________________ test_venv_and_pths ____________________________________________________________________________
    
    venv_path = '/tmp/pytest-of-tkloczko/pytest-1481/venv_path0/venv'
    
        def test_venv_and_pths(venv_path):
            pjoin = os.path.join
    
            site_pkg_path = pjoin(venv_path, 'lib')
            if os.name == 'nt':
                site_pkg_path = pjoin(site_pkg_path, 'site-packages')
            else:
                site_pkg_path = glob(pjoin(site_pkg_path, 'python*', 'site-packages'))[0]
            shutil.rmtree(site_pkg_path)
            shutil.copytree(get_example_dir('sample_venvs', 'pth_directory'), site_pkg_path)
    
            virtualenv = create_environment(venv_path)
            venv_paths = virtualenv.get_sys_path()
    
            ETALON = [
                # For now disable egg-links. I have no idea how they work... ~ dave
                #pjoin('/path', 'from', 'egg-link'),
                #pjoin(site_pkg_path, '.', 'relative', 'egg-link', 'path'),
                site_pkg_path,
                pjoin(site_pkg_path, 'dir-from-foo-pth'),
                '/foo/smth.py:module',
                # Not sure why it's added twice. It has to do with site.py which is not
                # something we can change. However this obviously also doesn't matter.
                '/foo/smth.py:from_func',
                '/foo/smth.py:from_func',
            ]
    
            # Ensure that pth and egg-link paths were added.
    >       assert venv_paths[-len(ETALON):] == ETALON
    E       AssertionError: assert ['/tmp/pytest...py:from_func'] == ['/tmp/pytest...py:from_func']
    E         At index 2 diff: '/foo/smth.py:from_func' != '/foo/smth.py:module'
    E         Use -v to get more diff
    
    test/test_inference/test_sys_path.py:61: AssertionError
    ========================================================================= short test summary info ==========================================================================
    SKIPPED [86] test/test_integration.py:45: Needs django to be installed to run this test.
    SKIPPED [3] test/test_api/test_environment.py:38: Skipped
    SKIPPED [1] test/test_inference/test_docstring.py:176: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:193: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:209: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:228: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:246: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:261: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:283: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:304: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:326: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:347: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:368: numpydoc module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:390: numpydoc or numpy module is unavailable
    SKIPPED [1] test/test_inference/test_docstring.py:402: numpydoc or numpy module is unavailable
    SKIPPED [2] test/test_inference/test_sys_path.py:70: condition: sys.platform!='win32'
    XFAIL test/test_inference/test_imports.py::test_flask_ext[import flask.ext.foo; flask.ext.foo.-Foo]
    XFAIL test/test_inference/test_imports.py::test_flask_ext[import flask.ext.bar; flask.ext.bar.-Foo]
    XFAIL test/test_inference/test_imports.py::test_flask_ext[import flask.ext.baz; flask.ext.baz.-Foo]
    XFAIL test/test_inference/test_imports.py::test_flask_ext[import flask.ext.moo; flask.ext.moo.-Foo]
    XFAIL test/test_inference/test_precedence.py::test_equals[... == ...]
    FAILED test/test_inference/test_sys_path.py::test_venv_and_pths - AssertionError: assert ['/tmp/pytest...py:from_func'] == ['/tmp/pytest...py:from_func']
    ==================================================== 1 failed, 3753 passed, 104 skipped, 5 xfailed in 145.10s (0:02:25) ====================================================
    

    Here is list of installed modules in build env

    Package                       Version
    ----------------------------- -----------------
    alabaster                     0.7.12
    appdirs                       1.4.4
    attrs                         22.1.0
    Babel                         2.11.0
    Brlapi                        0.8.3
    build                         0.9.0
    charset-normalizer            3.0.1
    colorama                      0.4.6
    contourpy                     1.0.6
    cssselect                     1.1.0
    cycler                        0.11.0
    distro                        1.8.0
    dnspython                     2.2.1
    docopt                        0.6.2
    docutils                      0.19
    exceptiongroup                1.0.0
    extras                        1.0.0
    fixtures                      4.0.0
    fonttools                     4.38.0
    gpg                           1.17.1-unknown
    idna                          3.4
    imagesize                     1.4.1
    importlib-metadata            5.1.0
    iniconfig                     1.1.1
    Jinja2                        3.1.2
    kiwisolver                    1.4.4
    libcomps                      0.1.19
    louis                         3.23.0
    lxml                          4.9.1
    MarkupSafe                    2.1.1
    matplotlib                    3.6.2
    numpy                         1.23.1
    olefile                       0.46
    packaging                     21.3
    parso                         0.8.3
    path                          16.5.0
    pbr                           5.9.0
    pep517                        0.13.0
    Pillow                        9.3.0
    pip                           22.3.1
    pluggy                        1.0.0
    Pygments                      2.13.0
    PyGObject                     3.42.2
    pyparsing                     3.0.9
    pytest                        7.2.0
    python-dateutil               2.8.2
    pytz                          2022.4
    requests                      2.28.1
    rpm                           4.17.0
    scour                         0.38.2
    setuptools                    65.6.3
    six                           1.16.0
    snowballstemmer               2.2.0
    Sphinx                        5.3.0
    sphinx-rtd-theme              1.1.1
    sphinxcontrib-applehelp       1.0.2.dev20220730
    sphinxcontrib-devhelp         1.0.2.dev20220730
    sphinxcontrib-htmlhelp        2.0.0
    sphinxcontrib-jsmath          1.0.1.dev20220730
    sphinxcontrib-qthelp          1.0.3.dev20220730
    sphinxcontrib-serializinghtml 1.1.5
    testtools                     2.5.0
    tomli                         2.0.1
    urllib3                       1.26.12
    wheel                         0.38.4
    zipp                          3.11.0
    
    opened by kloczek 3
  • [Question] Getting class of object on which completion was requested

    [Question] Getting class of object on which completion was requested

    Hi, I am developing tab completion feature for a Python library and have found jedi to be quite useful. My use case is to detect the class name of the object on which completion was requested. Is there a clean way to achieve this? I could find below workaround, here the goal is to find that x is of type Z.

    from jedi import Interpreter
    
    class Z:
        def foo(self): pass
    
    code = """
    x = Z()
    x.
    """
    
    interpreter = Interpreter(code, [locals()])
    completions = interpreter.complete(line=3, column=2)
    foo = completions[0].full_name  # completion for function foo, full name is '__main__.Z.foo'
    class_name = '.'.join(foo.split('.')[:-1])
    class_name
    

    Thanks!

    discussion 
    opened by nishikantparmariam 3
  • get_signature() for decorated function faulty with manipulated parameter list

    get_signature() for decorated function faulty with manipulated parameter list

    I can reproduce #1791 with Jedi 0.18.1 and Python 3.10.3 using a different approach:

    import functools
    
    def f(x: int, y: float):
        pass
    
    def do_wrap():
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            x = args[0]
            y = args[1]
            args = (x,y)
            return f(*args, **kwargs)
        return wrapper
    
    fails = do_wrap()
    fails(1, 2.0) # f(*args)
    

    So basically the problem occurs when passing different parameters (e.g. different arg object) to f() within wrapper(). So calling the function like that f(x, y) also fails.

    Motivation: I have a decorator that corrects some args of my wrapped functions (based on different rules) before calling its implementation.

    performance 
    opened by vbrdev 4
Safe code refactoring for modern Python.

Safe code refactoring for modern Python projects. Overview Bowler is a refactoring tool for manipulating Python at the syntax tree level. It enables s

Facebook Incubator 1.4k Jan 4, 2023
Bottom-up approach to refactoring in python

Introduction RedBaron is a python library and tool powerful enough to be used into IPython solely that intent to make the process of writing code that

Python Code Quality Authority 653 Dec 30, 2022
AST based refactoring tool for Python.

breakfast AST based refactoring tool. (Very early days, not usable yet.) Why 'breakfast'? I don't know about the most important, but it's a good meal.

eric casteleijn 0 Feb 22, 2022
Refactoring Python Applications for Simplicity

Python Refactoring Refactoring Python Applications for Simplicity. You can open and read project files or use this summary ?? Concatenate String my_st

Mohammad Dori 3 Jul 15, 2022
IDE allow you to refactor code, Baron allows you to write refactoring code.

Introduction Baron is a Full Syntax Tree (FST) library for Python. By opposition to an AST which drops some syntax information in the process of its c

Python Code Quality Authority 278 Dec 29, 2022
Simple, hassle-free, dependency-free, AST based source code refactoring toolkit.

refactor is an end-to-end refactoring framework that is built on top of the 'simple but effective refactorings' assumption. It is much easier to write a simple script with it rather than trying to figure out what sort of a regex you need in order to replace a pattern (if it is even matchable with regexes).

Batuhan Taskaya 385 Jan 6, 2023
A system for Python that generates static type annotations by collecting runtime types

MonkeyType MonkeyType collects runtime types of function arguments and return values, and can automatically generate stub files or even add draft type

Instagram 4.1k Dec 28, 2022
A library that modifies python source code to conform to pep8.

Pep8ify: Clean your code with ease Pep8ify is a library that modifies python source code to conform to pep8. Installation This library currently works

Steve Pulec 117 Jan 3, 2023
Code generation and code search for Python and Javascript.

Codeon Code generation and code search for Python and Javascript. Similar to GitHub Copilot with one major difference: Code search is leveraged to mak

null 51 Dec 8, 2022
A simple Python bytecode framework in pure Python

A simple Python bytecode framework in pure Python

null 3 Jan 23, 2022
Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Tô Đức (Watson) 4 Feb 7, 2022
A tool (and pre-commit hook) to automatically add trailing commas to calls and literals.

add-trailing-comma A tool (and pre-commit hook) to automatically add trailing commas to calls and literals. Installation pip install add-trailing-comm

Anthony Sottile 264 Dec 20, 2022
Find dead Python code

Vulture - Find dead code Vulture finds unused code in Python programs. This is useful for cleaning up and finding errors in large code bases. If you r

Jendrik Seipp 2.4k Dec 27, 2022
Programmatically edit text files with Python. Useful for source to source transformations.

massedit formerly known as Python Mass Editor Implements a python mass editor to process text files using Python code. The modification(s) is (are) sh

null 106 Dec 17, 2022
Tool for translation type comments to type annotations in Python

com2ann Tool for translation of type comments to type annotations in Python. The tool requires Python 3.8 to run. But the supported target code versio

Ivan Levkivskyi 123 Nov 12, 2022
Removes commented-out code from Python files

eradicate eradicate removes commented-out code from Python files. Introduction With modern revision control available, there is no reason to save comm

Steven Myint 146 Dec 13, 2022
Leap is an experimental package written to enable the utilization of C-like goto statements in Python functions

Leap is an experimental package written to enable the utilization of C-like goto statements in Python functions

null 6 Dec 26, 2022
Removes unused imports and unused variables as reported by pyflakes

autoflake Introduction autoflake removes unused imports and unused variables from Python code. It makes use of pyflakes to do this. By default, autofl

Steven Myint 678 Jan 4, 2023
A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.

pyupgrade A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language. Installation pip install pyupgrade As a pre

Anthony Sottile 2.4k Jan 8, 2023