Skip to content

Conversation

@rchen152
Copy link
Collaborator

I'm in the process of pulling a newer version of typeshed into Google
and seeing a lot of new errors due to code that passes 0 or 1 into
functions that are annotated as expecting bools.

Based on the fact that tk.{YES,NO,TRUE,FALSE} are defined as ints
(

NO: Literal[0]
YES: Literal[1]
TRUE: Literal[1]
FALSE: Literal[0]
)
and that the usage example in the source code also uses an int as a bool
(https://github.com/python/cpython/blob/cc75ab791dd5ae2cb9f6e0c3c5f734a6ae1eb2a9/Lib/tkinter/__init__.py#L25),
I think the annotations should be expanded.

I'm in the process of pulling a newer version of typeshed into Google
and seeing a lot of new errors due to code that passes 0 or 1 into
functions that are annotated as expecting bools.

Based on the fact that tk.{YES,NO,TRUE,FALSE} are defined as ints
(https://github.com/python/typeshed/blob/20a847218ac085ad691ea8d99155ee8ac7005479/stdlib/3/tkinter/constants.pyi#L3-L6)
and that the usage example in the source code also uses an int as a bool
(https://github.com/python/cpython/blob/cc75ab791dd5ae2cb9f6e0c3c5f734a6ae1eb2a9/Lib/tkinter/__init__.py#L25),
I think the annotations should be expanded.
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

+ porcupine/plugins/overview.py:37: error: No overload variant matches argument types "int", "bool", "bool", "Callable[[VarArg(object)], None]", "str"
+ porcupine/plugins/overview.py:37: note:     <1 more non-matching overload not shown>
+ porcupine/plugins/overview.py:37: note:     def configure(self, cnf: Optional[Dict[str, Any]] = ..., *, autoseparators: bool = ..., background: str = ..., bd: Union[str, float] = ..., bg: str = ..., blockcursor: bool = ..., border: Union[str, float] = ..., borderwidth: Union[str, float] = ..., cursor: Union[str, Tuple[str], Tuple[str, str], Tuple[str, str, str], Tuple[str, str, str, str]] = ..., endline: Union[int, Literal['']] = ..., exportselection: bool = ..., fg: str = ..., font: Union[str, Font, Tuple[str, int], Tuple[str, int, str], Tuple[str, int, Union[List[str], Tuple[str, ...]]]] = ..., foreground: str = ..., height: Union[str, float] = ..., highlightbackground: str = ..., highlightcolor: str = ..., highlightthickness: Union[str, float] = ..., inactiveselectbackground: str = ..., insertbackground: str = ..., insertborderwidth: Union[str, float] = ..., insertofftime: int = ..., insertontime: int = ..., insertunfocussed: Union[Literal['none'], Literal['hollow'], Literal['solid']] = ..., insertwidth: Union[str, float] = ..., maxundo: int = ..., padx: Union[str, float] = ..., pady: Union[str, float] = ..., relief: Union[Literal['raised'], Literal['sunken'], Literal['flat'], Literal['ridge'], Literal['solid'], Literal['groove']] = ..., selectbackground: str = ..., selectborderwidth: Union[str, float] = ..., selectforeground: str = ..., setgrid: bool = ..., spacing1: Union[str, float] = ..., spacing2: Union[str, float] = ..., spacing3: Union[str, float] = ..., startline: Union[int, Literal['']] = ..., state: Union[Literal['normal'], Literal['disabled']] = ..., tabs: Union[Union[str, float], str, Tuple[Union[Union[str, float], str], ...]] = ..., tabstyle: Union[Literal['tabular'], Literal['wordprocessor']] = ..., takefocus: Union[Literal[''], Callable[[str], Optional[int]]] = ..., undo: bool = ..., width: int = ..., wrap: Union[Literal['none'], Literal['char'], Literal['word']] = ..., xscrollcommand: Union[str, Callable[[float, float], Any]] = ..., yscrollcommand: Union[str, Callable[[float, float], Any]] = ...) -> Optional[Dict[str, Tuple[str, str, str, Any, Any]]]
+ porcupine/plugins/overview.py:37: note: Possible overload variant:
+ more_plugins/tetris.py:227: error: Argument "takefocus" to "Canvas" has incompatible type "Literal[True]"; expected "Union[Literal[''], Callable[[str], Optional[int]]]"

Literal[True] doesn't seem to match Optional[int]?
I derped, didn't realize that Optional[int] was part of the Callable,
not its own type.
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

While I think this is a bit unfortunate per the reasoning in #4634 and I hope this instance will get fixed in Python at some point, I agree that in this case it's best to use int (for now).

@srittau srittau merged commit a42f545 into master Nov 13, 2020
@srittau srittau deleted the tkinter branch November 13, 2020 10:34
Akuli added a commit to Akuli/typeshed that referenced this pull request Jan 5, 2021
Akuli added a commit to Akuli/typeshed that referenced this pull request Jan 5, 2021
Akuli added a commit to Akuli/typeshed that referenced this pull request Jan 5, 2021
Akuli added a commit to Akuli/typeshed that referenced this pull request Jan 5, 2021
Akuli added a commit to Akuli/typeshed that referenced this pull request Feb 10, 2024
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