Skip to content

Chore: Added type hints to searches/binary_search.py #2682

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

Merged
merged 5 commits into from
Dec 9, 2020

Conversation

UmairKamran
Copy link
Contributor

@UmairKamran UmairKamran commented Oct 3, 2020

Describe your change:

Added missing type hints as described in this issue -> #2128

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@poyea poyea added the hacktoberfest-accepted Accepted to be counted towards Hacktoberfest label Oct 5, 2020
Copy link

@hemanth-kotagiri hemanth-kotagiri left a comment

Choose a reason for hiding this comment

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

LGTM.
@Panquesito7, I approve of these changes. Please look into it.

@Panquesito7
Copy link
Member

Please look into it.

Sorry, I'm not a Python developer/maintainer.
@cclauss.

@stale
Copy link

stale bot commented Dec 9, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Dec 9, 2020
@UmairKamran
Copy link
Contributor Author

LGTM.
@Panquesito7, I approve of these changes. Please look into it.

Hey @hemanth-kotagiri, how can we get this merged? It's becoming stale

@stale stale bot removed the stale Used to mark an issue or pull request stale. label Dec 9, 2020
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Dec 9, 2020
@dhruvmanila dhruvmanila self-requested a review December 9, 2020 10:46
@hemanth-kotagiri
Copy link

@UmairKamran, I do not have the rights for this repository yet to merge your PR. What I'd suggest you is the email or ping @cclauss for the same.

Thanks!

@dhruvmanila
Copy link
Member

Closing and reopening this pull request to run checks done by the bot.

@dhruvmanila dhruvmanila closed this Dec 9, 2020
@dhruvmanila dhruvmanila reopened this Dec 9, 2020
@ghost ghost added the enhancement This PR modified some existing files label Dec 9, 2020
Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Sorry for the delay. Small changes and this is good to go!

@@ -219,7 +228,7 @@ def binary_search(sorted_collection, item):
return None


def binary_search_std_lib(sorted_collection, item):
def binary_search_std_lib(sorted_collection, item) -> Optional[int]:
Copy link
Member

Choose a reason for hiding this comment

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

I think you missed a few in here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Looks like @cclauss has taken care of this

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Dec 9, 2020
@cclauss
Copy link
Member

cclauss commented Dec 9, 2020

I will fix it and land it in the next few hours. Instead of parameters that can be either int or None, it would be better if they were only int where we use -1 as the signal value rather than None.

@dhruvmanila
Copy link
Member

Ah, so you want to use the Sentinel pattern? There's a good article about it: https://python-patterns.guide/python/sentinel-object/

Any reason why you are preferring for an int instead of None? None is a good indication for the absence of value unless None is actually a valid value for the given parameter which is where the Sentinel object pattern is used.

@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Dec 9, 2020
@ghost ghost added tests are failing Do not merge until tests pass and removed awaiting reviews This PR is ready to be reviewed labels Dec 9, 2020
@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed tests are failing Do not merge until tests pass labels Dec 9, 2020
@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Dec 9, 2020
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Dec 9, 2020
@UmairKamran
Copy link
Contributor Author

@dhruvmanila @cclauss Thank you for reviewing :)

@cclauss cclauss merged commit bd4b83f into TheAlgorithms:master Dec 9, 2020
@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Dec 9, 2020
@cclauss
Copy link
Member

cclauss commented Dec 9, 2020

Any reason why you are preferring for an int instead of None?

The expected datatype is int so why make things more complicated if we can find a suitable sentinel value of that datatype? The current project that I am working on overuses None throughout the codebase. This complicates the addition of type checking (with Optional) so many contributors just avoid using type checking which is hurting our ability to spot bugs.

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
)

* Chore: Added type hints to searches/binary_search.py

* Use -1 as the sentinal value

* Wrap long lines

* Update binary_search.py

* Update binary_search.py

Co-authored-by: Christian Clauss <cclauss@me.com>
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
)

* Chore: Added type hints to searches/binary_search.py

* Use -1 as the sentinal value

* Wrap long lines

* Update binary_search.py

* Update binary_search.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
)

* Chore: Added type hints to searches/binary_search.py

* Use -1 as the sentinal value

* Wrap long lines

* Update binary_search.py

* Update binary_search.py

Co-authored-by: Christian Clauss <cclauss@me.com>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
)

* Chore: Added type hints to searches/binary_search.py

* Use -1 as the sentinal value

* Wrap long lines

* Update binary_search.py

* Update binary_search.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files hacktoberfest-accepted Accepted to be counted towards Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants