Skip to content

Commit

Permalink
Fix some ignored type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Butcher committed Dec 11, 2021
1 parent 448ff36 commit 0e46179
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
13 changes: 10 additions & 3 deletions pretf.aws/pretf/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
from time import sleep
from typing import Any, Callable, Optional

from boto3 import Session

from pretf.api import block, log
from pretf.blocks import Block

try:
from boto_source_profile_mfa import get_session as Session # type: ignore
import boto_source_profile_mfa

use_boto_source_profile_mfa = True
except ImportError:
from boto3 import Session # type: ignore
use_boto_source_profile_mfa = False


# Use this lock on anything that might trigger an MFA prompt,
Expand Down Expand Up @@ -239,7 +243,10 @@ def get_frozen_credentials(

@lru_cache()
def get_session(**kwargs: Any) -> Session:
return Session(**kwargs)
if use_boto_source_profile_mfa:
return boto_source_profile_mfa.get_session(**kwargs)
else:
return Session(**kwargs)


@locked
Expand Down
2 changes: 1 addition & 1 deletion pretf.aws/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def get_version():
author_email="randomy@gmail.com",
license="MIT License",
packages=["pretf"],
install_requires=["boto3", "pretf"],
install_requires=["boto3", "boto3-stubs", "pretf"],
zip_safe=False,
)
19 changes: 15 additions & 4 deletions pretf/pretf/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from json import loads as json_loads
from pathlib import Path
from subprocess import CompletedProcess
from typing import Any, Callable, Dict, Generator, List, Optional, Union
from types import TracebackType
from typing import Any, Callable, Dict, Generator, List, Optional, Type, Union

import pytest

Expand Down Expand Up @@ -35,7 +36,12 @@ def __call__(self, cwd: Union[Path, str] = "") -> "TerraformProxy":
def __enter__(self) -> "TerraformProxy":
return self

def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
return None

# Terraform command.
Expand Down Expand Up @@ -134,7 +140,12 @@ def execute(self, *args: str) -> CompletedProcess:


class SimpleTestMeta(type):
def __new__(cls, name, bases, dct): # type: ignore
def __new__(
cls: Type["SimpleTestMeta"],
name: str,
bases: tuple,
dct: dict,
) -> "SimpleTestMeta":
"""
Wraps all test methods with the pretf_test_function() decorator.
Expand All @@ -146,7 +157,7 @@ def __new__(cls, name, bases, dct): # type: ignore

return super().__new__(cls, name, bases, dct)

def __init__(self, name, bases, dct): # type: ignore
def __init__(self, name: str, bases: tuple, dct: dict) -> None:
"""
Adds any test method using the @always decorator to cls._always
so the pretf_test_function() can run it even when previous tests
Expand Down
2 changes: 1 addition & 1 deletion pretf/pretf/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def proxy(self, consumer: Any) -> VariableProxy:

class TerraformVariableStore(VariableStore):
def __init__(self, files_to_create: dict) -> None:
super().__init__() # type: ignore
super().__init__()
self._files_to_create = files_to_create
self._files_done: Set[Path] = set()
self._tfvars_waiting: Set[Path] = set()
Expand Down
6 changes: 4 additions & 2 deletions pretf/pretf/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ def custom(

with import_file(path) as module:

if not hasattr(module, "pretf_workflow"):
try:
func = getattr(module, "pretf_workflow")
except AttributeError:
raise log.bad(f"workflow: {path} does not have a 'pretf_workflow' function")

# Call the pretf_workflow() function,
# passing in "path" and "terraform" if required.
result = call_pretf_function(func=module.pretf_workflow, context=context) # type: ignore
result = call_pretf_function(func=func, context=context)

if isinstance(result, int):
result = CompletedProcess(args=[str(path)], returncode=result)
Expand Down
5 changes: 5 additions & 0 deletions stubs/boto_source_profile_mfa.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Any

import boto3

def get_session(**kwargs: Any) -> boto3.Session: ...

0 comments on commit 0e46179

Please sign in to comment.