How to get PyTest fixtures to autocomplete in PyCharm (type hinting) : <p>I had a bear of a time figuring this out, and it was really bugging me, so I thought I'd post this here in case anyone hit the same problem...</p>

<p>(and the answer is so dang simple it hurts :-)</p>

<p><strong>The Problem</strong></p>

<p>The core of the issue is that sometimes, not always, when dealing with fixtures in PyTest that return objects, when you use those fixtures in a test in PyCharm, you don't get autocomplete hints. If you have objects with large numbers of methods you want to reference while writing a test, this can add a lot of overhead and inconvenience to the test writing process. </p>

<p>Here's a simple example to illustrate the issue:</p>

<p>Let's say I've got a class "event_manager" that lives in:</p>

<pre><code>location.game.events
</code></pre>

<p>Let's further say that in my conftest.py file (PyTest standard thing for the unfamiliar), I've got a fixture that returns an instance of that class:</p>

<pre><code>from location.game.events import event_manager

...

@pytest.fixture(scope="module")
def event_mgr():
    """Creates a new instance of event generate for use in tests"""
    return event_manager()
</code></pre>

<p>I've had issues sometimes, (but not always - I can't quite figure out why) with classes like this where autocomplete will not work properly in the test code where I use the fixture, e.g.</p>

<pre><code>def test_tc10657(self, evt_mgr):
    """Generates a Regmod and expects filemod to be searchable on server"""
    evt_mgr.(This does not offer autocomplete hints when you type ".")
</code></pre>

<p>So the answer is actually quite simple, once you review type hinting in PyCharm:
<a href="http://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html" rel="noreferrer">http://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html</a></p>

<p>Here's how to fix the above test code so that autocomplete works properly:</p>

<pre><code>from location.game.events import event_manager 

...

def test_tc10657(self, evt_mgr: event_manager):
    """Generates a Regmod and expects filemod to be searchable on server"""
    evt_mgr.(This DOES offer hints when you type "." Yay!)
</code></pre>

<p>Notice how I explicitly type the fixture as an input parameter of type event_manager.</p>
