kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Allow debug_stream to be set optionally #393

Closed azjps closed 5 months ago

azjps commented 1 year ago

First, a thank you for the project; I've been looking into integrating argcomplete into ipython/traitlets#811 so that shell completion works "out-of-the-box" for IPython, Jupyter, and everything else in the ecosystem.

Currently in CompletionFinder.__init__(), the following is done:

        global debug_stream
        try:
            debug_stream = os.fdopen(9, "w")
        except Exception:
            debug_stream = sys.stderr
        debug()

Could this behavior be made optional? I think it could either be extracted out into a separate helper method that can be overridden, or perhaps be controlled by either an additional argument (similar to output_stream=) or environment variable. Feel free to let me know which (or both) is preferable, and I can open a PR.

The problem is that this file descriptor could be used elsewhere, for example by pytest, leading to obscure failures. You can find a MRE in https://github.com/ipython/traitlets/pull/815/commits/2654e3feb9e152148a519c47a73200b9bd683133. I was able to get around this for now by mock-ing os.fdopen https://github.com/ipython/traitlets/pull/811/commits/0deadccc0adc0d9a91a15fe8790f2b7c5c8831a9, but I'd prefer not to have to introduce mocking if possible.

I think this was also identified as one of two issues in #365 but was closed since the other half was resolved.

kislyuk commented 1 year ago

Thanks for looking into this. I am open to making the debug stream initialization optional by breaking it out into a method that can be disabled in subclasses. Would you be interested in creating a PR for that?