pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Cannot import certain testinfra classes for Type Annotations

Open Kilo59 opened this issue 5 years ago • 2 comments

I would like to be able to use testinfra classes (Specifically Package and CommandResults) in type annotations.

def my_function(pkg_names: List[str]) -> List[testinfra.modules.base.Package]:
   # function code that returns a list containing of Package instance objects:

However I always get an error AttributeError: module 'testinfra.modules' has no attribute 'base'.

I'm able to import and use Hosts without issue.

Kilo59 avatar Nov 09 '18 17:11 Kilo59

Hi, did you tried List[testinfra.modules.package.Package] ?

philpep avatar Nov 12 '18 13:11 philpep

Python 3.6.6 (default, Sep 12 2018, 18:26:19)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import testinfra
>>> testinfra.modules.package.Package
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'testinfra.modules' has no attribute 'package'

I altered my code so in my specific case I no longer need use Package in the return type annotation.


I would still really like to be able to import the CommandResult type so I can use it for Type checking and hinting.

For CommandResult I tried testinfra.backend.base.CommandResult but I get a no 'base' attribute error. If I do a dir() on testinfra.backend I see things like... 'BACKENDS', 'get_backend', 'get_backend_class', 'get_backends'. I assumed I could use one of those to access the CommandResult class.

This is what I got when I tried to use get_backend_class().

>>> testinfra.backend.get_backend_class("CommandResults")
Traceback (most recent call last):
  File "/path_to_my_project-P6MEBeTa/lib/python3.6/site-packages/testinfra/backend/__init__.py", line 37, in get_backend_class
    classpath = BACKENDS[connection]
KeyError: 'CommandResults'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "path_to_my_project-P6MEBeTa/lib/python3.6/site-packages/testinfra/backend/__init__.py", line 39, in get_backend_class
    raise RuntimeError("Unknown connection type '%s'" % (connection,))
RuntimeError: Unknown connection type 'CommandResults'

Kilo59 avatar Nov 13 '18 13:11 Kilo59