-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Annotate normal_form_game.py #576
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
base: main
Are you sure you want to change the base?
Conversation
Hello @rht! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:
|
Will fix the line too long issues later. |
There is never such case, from the way |
In |
Yes, it happens when There are two options:
Maybe option 1? |
There is a |
Maybe it is safer to tell the user there is a problem instead of |
@@ -233,7 +240,7 @@ def delete_action(self, action, player_idx=0): | |||
payoff_array_new = np.delete(self.payoff_array, action, player_idx) | |||
return Player(payoff_array_new) | |||
|
|||
def payoff_vector(self, opponents_actions): | |||
def payoff_vector(self, opponents_actions: npt.ArrayLike) -> np.ndarray: |
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.
opponents_actions
should have been IntOrArray
, which is Union[int, npt.ArrayLike]
.
But doing so will cause problem in
reduce_last_player(payoff_vector, opponents_actions[i]) |
Union[int, ...]
type generally can't be indexed. Alternative solution to my current version is to use cast()
before the indexing to cast opponents_actions
into an indexable.
Note that int
is also part of npt.ArrayLike
, since np.array(1)
works.
thanks @rht sorry for my slow response.
@oyamad should we add an exception if |
Actually, this function |
So your annotations are correct. |
|
@oyamad are you happy with these annotations? |
I think it is safe to replace all of QuantEcon.py/quantecon/game_theory/normal_form_game.py Lines 258 to 261 in 538bf7c
|
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.
Pull Request Overview
This PR adds type annotations to the normal_form_game.py
module to improve type safety and developer experience. The changes include comprehensive type hints for function parameters, return types, and class attributes while also fixing a potential bug where a function could return None
instead of the expected int
.
- Adds comprehensive type annotations throughout the Player and NormalFormGame classes
- Introduces a custom type alias
IntOrArrayT
for union types commonly used in the module - Fixes a missing return statement bug in the
best_response_2p
function
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
def best_response_2p(payoff_matrix, opponent_mixed_action, tol=1e-8): | ||
def best_response_2p( | ||
payoff_matrix: np.ndarray, opponent_mixed_action: np.ndarray, tol: float = 1e-8 | ||
) -> int: |
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.
The function best_response_2p
has a missing return statement. Based on the PR description, there's a case where the function can return None
instead of int
when the if condition is never satisfied. The function needs a proper return statement to handle all code paths.
Copilot uses AI. Check for mistakes.
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.
The annotation here is quite old (from typing import List
instead of using list
). Can you also highlight outdated patterns in this PR?
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.
@copilot see my previous message.
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.
np.ndarray
is a catch-all type for all ndarrays regardless of its dtype, and so, is unfortunately less specific than the docstring that specifies ndarray.action_profile
since the functions using it are not documented.normal_form_game.py:921: error: Missing return statement
inQuantEcon.py/quantecon/game_theory/normal_form_game.py
Line 931 in 6ffbe57
None
instead ofint
when the if condition is never satisfied.