-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get sydent.http.auth
passing mypy --strict
#433
Conversation
(linter doesn't look at stubs, it seems)
sydent/http/auth.py
Outdated
@@ -82,6 +78,7 @@ def authV2( | |||
|
|||
if requireTermsAgreed: | |||
terms = get_terms(sydent) | |||
assert terms is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure at this point that terms
is not None
? It's possible to run Python code in a configuration that ignore assert
statements so we're trying to limit our use of them to double checking conditions we're reasonably sure we're not going to hit. If there's a realistic possibility of terms
being None
could you raise
normally instead please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
terms is None
if and only if the try
block in get_terms
raises an Exception
. That gets immediately caught, logged and suppressed.
Lines 120 to 160 in 5de7349
def get_terms(sydent: "Sydent") -> Optional[Terms]: | |
"""Read and parse terms as specified in the config.""" | |
# TODO - move some of this to parse_config | |
termsPath = sydent.config.general.terms_path | |
try: | |
if termsPath == "": | |
return Terms(None) | |
with open(termsPath) as fp: | |
termsYaml = yaml.safe_load(fp) | |
# TODO use something like jsonschema instead of this handwritten code. | |
if "master_version" not in termsYaml: | |
raise Exception("No master version") | |
elif not isinstance(termsYaml["master_version"], str): | |
raise TypeError( | |
f"master_version should be a string, not {termsYaml['master_version']!r}" | |
) | |
if "docs" not in termsYaml: | |
raise Exception("No 'docs' key in terms") | |
for docName, doc in termsYaml["docs"].items(): | |
if "version" not in doc: | |
raise Exception("'%s' has no version" % (docName,)) | |
if "langs" not in doc: | |
raise Exception("'%s' has no langs" % (docName,)) | |
for langKey, lang in doc["langs"].items(): | |
if "name" not in lang: | |
raise Exception( | |
"lang '%s' of doc %s has no name" % (langKey, docName) | |
) | |
if "url" not in lang: | |
raise Exception( | |
"lang '%s' of doc %s has no url" % (langKey, docName) | |
) | |
return Terms(termsYaml) | |
except Exception: | |
logger.exception("Couldn't read terms file '%s'", termsPath) | |
return None |
I could allow get_terms
to raise and have its return type be Terms?
Adding the stub for `getHeader` means that mypy isn't looking at twisted's implementation to deduce what types instance attributes have. So let's add those to the stub. I don't want to maintain a stub out-of-tree forever, but I've submitted the `getHeader` annotation upstream in twisted/twisted#1669 .
No description provided.