Skip to content

[mypy] Fix type annotations for stack.py #5566

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 1 commit into from
Oct 26, 2021

Conversation

archaengel
Copy link
Contributor

@archaengel archaengel commented Oct 23, 2021

$ git checkout mypy-fix-stacks-stack
Switched to branch 'mypy-fix-stacks-stack'

$ mypy --ignore-missing-imports data_structures/stacks/stack.py --strict
Success: no issues found in 1 source file

$ mypy --ignore-missing-imports data_structures/stacks --strict > after.txt

$ git checkout master
Switched to branch 'master'

$ mypy --ignore-missing-imports data_structures/stacks --strict > before.txt

$ diff before.txt after.txt
39,49d38
< data_structures/stacks/stack.py:31: error: Function is missing a type annotation
< data_structures/stacks/stack.py:37: error: Function is missing a return type annotation
< data_structures/stacks/stack.py:50: error: Function is missing a return type annotation
< data_structures/stacks/stack.py:74: error: Function is missing a type annotation for one or more arguments
< data_structures/stacks/stack.py:90: error: Call to untyped function "pop" in typed context
< data_structures/stacks/stack.py:96: error: Call to untyped function "peek" in typed context
< data_structures/stacks/stack.py:103: error: Call to untyped function "push" in typed context
< data_structures/stacks/stack.py:109: error: Call to untyped function "pop" in typed context
< data_structures/stacks/stack.py:110: error: Call to untyped function "peek" in typed context
< data_structures/stacks/stack.py:112: error: Call to untyped function "push" in typed context
< data_structures/stacks/stack.py:116: error: Call to untyped function "push" in typed context
52,62d40
< data_structures/stacks/dijkstras_two_stack_algorithm.py:60: error: Call to untyped function "push" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:63: error: Call to untyped function "push" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:66: error: Call to untyped function "peek" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:67: error: Call to untyped function "pop" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:68: error: Call to untyped function "peek" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:69: error: Call to untyped function "pop" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:70: error: Call to untyped function "peek" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:71: error: Call to untyped function "pop" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:74: error: Call to untyped function "push" in typed context
< data_structures/stacks/dijkstras_two_stack_algorithm.py:77: error: Returning Any from function declared to return "int"
< data_structures/stacks/dijkstras_two_stack_algorithm.py:77: error: Call to untyped function "peek" in typed context
75,85c53
< data_structures/stacks/balanced_parentheses.py:21: error: Call to untyped function "push" in typed context
< data_structures/stacks/balanced_parentheses.py:23: error: Call to untyped function "pop" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:47: error: Call to untyped function "push" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:49: error: Call to untyped function "peek" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:50: error: Call to untyped function "pop" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:51: error: Call to untyped function "pop" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:53: error: Call to untyped function "peek" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:54: error: Call to untyped function "pop" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:55: error: Call to untyped function "push" in typed context
< data_structures/stacks/infix_to_postfix_conversion.py:57: error: Call to untyped function "pop" in typed context
< Found 82 errors in 12 files (checked 13 source files)
---
> Found 50 errors in 8 files (checked 13 source files)

Related to #4052

Describe your change:

Add generic type annotations to functions in stack.py. Add type annotations to variables of Stack class.

  • 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}.

@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 23, 2021
@archaengel archaengel marked this pull request as draft October 23, 2021 18:23
@archaengel archaengel force-pushed the mypy-fix-stacks-stack branch 2 times, most recently from 26744f4 to 0b32aae Compare October 24, 2021 06:51
@archaengel archaengel marked this pull request as ready for review October 24, 2021 06:52
@archaengel archaengel force-pushed the mypy-fix-stacks-stack branch from 0b32aae to 0934e14 Compare October 24, 2021 07:17
@@ -80,7 +84,7 @@ def test_stack() -> None:
"""
>>> test_stack()
"""
stack = Stack(10)
stack: Stack[int] = Stack(10)
Copy link
Member

Choose a reason for hiding this comment

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

I thought that the reason that we used Stack(Generic[T]) instead of Stack(Any) was so that we did not need to declare the type of each instance at creation time.

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Oct 26, 2021
@cclauss cclauss merged commit c0ed031 into TheAlgorithms:master Oct 26, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants