• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Python Navigation in Emacs

    April 28th, 2017
    code, python, tech  [html]
    I'm now working full time in python in a medium-sized existing codebase, and I've started using jedi-mode for code navigation. What I really like about this package is that I can press C-c . to jump to the definition of something, and then C-c , to jump back again. What's especially cool about jedi is that it parses the python syntax tree, understands type hints, and can show you the definitions of things like attributes on arbitrary objects. Consider:

    class Foo():
      def a():
        return 'foo'
    class Bar():
      def a():
        return 'bar'
    def baz(b: Foo) -> str:
      return b.a
    

    In a simple tags or dumb-jump system there's no way to know that b.a is Foo.a and not Bar.a, but jedi-mode understands the python and jumps to the right place.

    What I don't like about jedi-mode is that it also turns on other distracting things that get in the way of trying to write. If you hold the cursor still it pops up "quick help" that obscures the code below:

    If you start to type something it recognizes it offers a completion, and if you press the wrong key you'll accidentally accept the completion:

    And if you type something ambigous it can do both frustrating things at once:

    I put up with this for a surprisingly long time before looking into fixing it, but these can be disabled. The key thing is that jedi-mode turns on auto-complete-mode, and it's auto-complete that's responsible for most of the behavior I don't like. So: here's how to turn on jedi in a non-instrusive way:

    ; move quick-help tooltips to the minibuffer
    (setq jedi:tooltip-method nil)
    
    ; don't auto-complete unless requested
    (setq ac-auto-start nil)
    (setq ac-auto-show-menu nil)
    (define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
    
    (add-hook 'python-mode-hook 'jedi:setup)
    

    I'm pretty happy, but there are still two things I'd like:

    • C-c , on an empty stack should bring up a list of places a thing is used. That is, places where C-c . would bring you here.
    • Automated refactoring: rope has fallen way behind and can't handle python 3 codebases well. I'd love to be able to write "turn all calls to package.foo(...) into package._foo(...)" rules and have jedi automatically apply them.

    Comment via: google plus, facebook

    Recent posts on blogs I like:

    Who Should Bear the Risk in Infrastructure Projects?

    The answer to the question is the public sector, always. It’s okay to have private-sector involvement in construction, but the risk must be borne by the public sector, or else the private sector will just want more money to compensate for the extra risk. …

    via Pedestrian Observations November 30, 2020

    Fireside Friday, November 27, 2020

    Hey folks! Fireside this week. A bit of a change-up in terms of the coming attractions. I had planned to start “Textiles, How Did They Make It?” next, but I want to do a bit more reading on some of the initial stages of textile production (that is, the pr…

    via A Collection of Unmitigated Pedantry November 27, 2020

    Thoughts you mightn't have thunk about remote meetings

    Welcome to this week's edition of "building a startup in 2020," in which all your meetings are suddenly remote, and you probably weren't prepared for it. I know I wasn't. We started a "fully remote" company back in 2019, but …

    via apenwarr November 23, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact