Skip to content
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

Merged
merged 7 commits into from
Oct 22, 2021
Merged

Conversation

DMRobertson
Copy link
Contributor

No description provided.

David Robertson added 3 commits October 21, 2021 14:10
@DMRobertson DMRobertson requested a review from a team October 21, 2021 13:13
@babolivier babolivier self-assigned this Oct 21, 2021
@@ -82,6 +78,7 @@ def authV2(

if requireTermsAgreed:
terms = get_terms(sydent)
assert terms is not None
Copy link
Contributor

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?

Copy link
Contributor Author

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.

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?

David Robertson added 3 commits October 21, 2021 16:21
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 .
@DMRobertson DMRobertson merged commit a6fff7b into main Oct 22, 2021
@DMRobertson DMRobertson deleted the dmr/mypy/http/auth branch October 22, 2021 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants