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

Add type checks for initialiazion of models #1144

Closed
wants to merge 44 commits into from
Closed

Add type checks for initialiazion of models #1144

wants to merge 44 commits into from

Conversation

PythonCoderAS
Copy link
Contributor

Fixes # (provide issue number of applicable)

#1143: Adds in type checks to try and correct the errors being caused in the issue

@bboe
Copy link
Member

bboe commented Nov 28, 2019

Looks good. Did you run black on the code? Perhaps we have a version mismatch.

elif fullname:
self._fullname = fullname
if not isinstance(fullname, str):
raise TypeError(
"The id must be type `str`."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message should say fullname rather than id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it now, thanks

@PythonCoderAS
Copy link
Contributor Author

I am completely overhauling this part of the code, so I will close the PR until my features are finished.

@PythonCoderAS
Copy link
Contributor Author

I decided to add in a new feature decided to simplify the type checking process. It lives in praw/models/utils. Suggestions to move it anywhere?

@PythonCoderAS PythonCoderAS reopened this Dec 20, 2019
@PythonCoderAS
Copy link
Contributor Author

If you have any idea on how to make the function smaller, as it has a lot of if statements to cover every possibility of misuse by end-users. Although, end users are most likely not going to use this function, so I can remove the majority of the checks, such as for using None as an expected type.

@bboe
Copy link
Member

bboe commented Dec 21, 2019

@PokestarFan I appreciate all the effort you're putting into these changes. However, I worry it might be a bit overkill to type check every parameter. If someone wants that level of validation a dynamically typed language probably isn't for them. A better use of effort would be to use python's type hinting which is available in python 3.5+, which conveniently is what we support. Thoughts?

@PythonCoderAS
Copy link
Contributor Author

@bboe should I add in type hinting through stub files, or directly into the functions/classes?

@PythonCoderAS
Copy link
Contributor Author

For now I am going to only add in type checks to the public reddit methods such as submission.

@PythonCoderAS
Copy link
Contributor Author

@bboe @jackodsteel I have finished this PR. Every method that can be called from the reddit instance without accessing any attributes (such as Redditor, Comment, Submission, etc.) have been type checked. I did not implement type-checking onto any of the inbox methods, as those are methods of a class, not a callable class on the Reddit instance.

@bboe
Copy link
Member

bboe commented Dec 22, 2019

@PokestarFan I think I may not have been clear in my previous message. I don't see a ton of value in introducing explicit runtime type checks. Most issues should surface pretty quickly at runtime anyway if the wrong type is provided without said checks. While I understand that the exceptions as-is may be a little cryptic for people, the added complexity (2000+ lines for this PR, which is only a subset of methods) doesn't seem worth it. I think parameter type-hinting could help considerably for said cases and it can be done with relatively little effort.

Aside from that, the places where there exist some run-time checks are when parameters are mutually exclusive, or parameters must be grouped together. One could argue that in such cases, we might be better off making separate methods. While that's certainly a possibility, I haven't really heard any complaints about how it's currently done.

@PythonCoderAS
Copy link
Contributor Author

Understood. I will look into type-hinting, and work on mutually exclusive methods.

@PythonCoderAS
Copy link
Contributor Author

To anyone reading this later, I moved the code into PokestarFan/validate-types and force-pushed master to resemble master of praw

This was referenced Dec 23, 2019
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.

3 participants