Files
OpenBB/tests/formatting/test_best_practices.py
montezdesousa 732a614937 Allow users to change api keys through OpenBB SDK (#2642)
* set fred keys through api

* linting

* doc change

* expose api keys dict

* allow optional persistency of api key change

* black

* place decorators

* add keys paths

* av and fmp keys

* some fixes

* rewrite av, fmp, fred cassettes

* quandl key

* polygon key

* rewrite polygon cassette

* news and tradier

* cmc key

* change some docstring

* change some docstring

* filtered keys dict on mykeys

* simplify mykeys collection

* revert arg to persist

* finnhub

* iex key

* reddit key

* reddit docstring

* check for prefix before removing

* glassnode key

* glassnode virtual path

* bitquery keys

* twitter key

* robinhood key

* degiro keys

* oanda key

* binance keys

* si key

* coinbase key

* refactor btcrb dates and logic

* walert key

* coinglass key

* regenerate cassettes

* cpanic keys

* ethplorer keys

* replace ouput status failed msg

* smartstake keys

* gitub keys

* messari keys

* eodhd keys

* santiment keys

* remove unused imports

* typo formatting test

* remove forbidden arg

* fix btcrb test

* fix test import

* revert cassette changes

* fix more tests

* fix last failing

* black

* try to change status to int code

* change status to int

* first try at keys controller refactoring

* fix some tests accordingly

* display msg on set

* change some vars

* remove elifs

* add progress bar and remove duplicate key check

* add mykeys and refresh to menu

* remove unused import

* change mykeys output to df

* remove check keys from keys ctrl tests

* update test split to new structure

* reduce duplicate code

* upper case

* change var name

* rename mykeys df cols

* add keys integrations tests

* add mykeys test to keys controller

* fix mykeys

* fix wrong eodhd var name

* pylint

* test_set_key

* black

* patch dotenv kinda

* fix mypy

* remove refresh

* filter mykeys df

* filter mykeys df

* update integration tests

* first unit test for set api key

* add 2 more test cases to fred

* add persist test to fred unittest

* add av unittest

* add polygon unittest

* generate av cassettes

* add unittests av, fmp, quandl, polygon, fred

* change si token to key

* forgot controller func

* unittest: news, tradier, cmc, finnhub, iex, bitquery

* add si unittest

* update eodhd key name

* add unittest: walert, glassnode, coinglass, cpanic, ethplorer, github, messari, eodhd, santiment

* black

* add reddit unittest

* refactor unittests to handle multi args

* regenerate cassettes

* add more unittests

* add degiro, oanda and rh unittest

* generate oanda records

* fix rh and degiro unittest

* fix oanda unittest

* black

* flake8

* fix si controller test

* move tmp

* pylint

* fix av test

* fix fmp key check

* rewrite fmp

* rewrite oanda output

* fix quandl check

* rewrite expected

* supress logger exception

* fix iex cloud key

* remove iex warning

* fix coinglass check key

* cpanic fix

* regenerate cpanic cassettes

* flake8

* fix smartstake keys check

* regenerate expected output

* fix eodhd

* try at binance

* black

* fix controller tests

* pylint

* disable warning

* flake

* add option to show keys

* add Enum to model

* regenerate test output

* update contributing

* flake

* fix type hint

* remove status msg dict

* pylint

* regenerate and remove unecessary cassettes

* remove duplicate return

* supress traceback

* remove unused imports

* update some docstrings

* add todo

* add export option to mykeys

* minor change

* refactor color scheme

* update contributing

* update contributing

* regenerate cassettes

* fix check output

* small msg change

* small details

* rewrite some tests

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
2022-10-08 12:30:23 +02:00

88 lines
2.5 KiB
Python

from typing import Optional, List, Tuple
from inspect import signature
from docs.generate import all_functions
# A dictionary where keys are bad parameter names and values are the correct replacements
bad_params = {
"ticker": "symbol",
"descend": "ascend",
"descending": "ascend",
"ascending": "ascend",
"num": "limit",
"sort_by": "sortby",
"start": "start_date",
"end": "end_date",
"df": "data",
"x": "independent_series",
"y": "dependent_series",
"expire": "expiry",
"exp": "expiry",
"expiration": "expiry",
"vs": "to_symbol",
"coin": "symbol",
"coin_id": "symbol",
"token": "symbol",
"currency": "symbol",
"country_code": "country",
"sort_col": "sortby",
"sort_field": "sortby",
"num_tickers": "limit",
"num_stocks": "limit",
"since": "start_date",
"until": "end_date",
"top": "limit",
}
def create_overlaps(
overlaps: List[str], func_name: str, func_path: str
) -> Optional[Tuple[List[str], str, str, str]]:
"""
Checks if there is a bad parameter name in a function
Parameters
----------
overlaps : List[str]
Any overlaps between forbidden parameters and the function's parameters
func_name: str
The name of the function
func_path: str
The path for the function
Returns
----------
overlaps: Optional[Tuple[List[str], str, str, str]]
Tuple[overlaps, func_name, func_path, overlaps_str]
"""
if overlaps:
overlap_str = "\n"
for item in overlaps:
overlap_str += f"{item}->{bad_params[item]}\n"
return overlaps, func_name, func_path, overlap_str
return None
def test_bad_parameters():
all_funcs = all_functions()
total_overlaps = 0
all_overlaps: List[Tuple[List[str], str, str, str]] = []
for path, _, function in all_funcs:
params = signature(function)
overlaps = list(set(bad_params) & set(params.parameters.keys()))
new_overlaps = create_overlaps(overlaps, function.__name__, path)
if new_overlaps:
total_overlaps += 1
all_overlaps.append(new_overlaps)
final_str = ""
if all_overlaps:
for overlaps, func_name, func_path, overlap_str in all_overlaps:
final_str += (
f"Forbidden parameter{'s' if len(overlaps) > 1 else ''} in function"
f" '{func_name}' at {func_path}. Replace the following: {overlap_str}\n\n"
)
final_str += f"\n\nTotal bad functions: {total_overlaps}"
raise NameError(final_str)