13

If duplicate, please let me know, but I couldn't find an answer. Also, this question does not seem to address the issue.

I'd like to declare type of multiple variables in the same line. Instead of

a: float
b: float
c: float

I'd like to use something like

a, b, c: float

But I get a syntax error. What is the correct syntax?

4 Answers 4

17

It doesn't appear to be possible to annotate multiple variables with one annotation statement.

Annotated Assignment Statements defines an annotation as:

annotated_assignment_stmt ::=  augtarget ":" expression
                               ["=" (starred_expression | yield_expression)]

So the augtarget rule dictates what is allowed to go before the colon. augtarget is defined as:

augtarget                 ::=  identifier | attributeref | subscription | slicing

So the only things that can go before the colon is an identifier (i.e. a single variable), an attributeref (an expression followed by .some_attribute_name), a subscription (an expression followed by [some_index]), or a slicing (same syntax as a subscription). a, b, c is not any of these things, so a, b, c: <some type> is not legal syntax.


If you merely want to annotate all three variables on one line, and not necessarily in one statement, you can chain independent simple statements together with a semicolon:

a:float; b:float; c:float

... But this is somewhat unsatisfying since you still have to type float three times.

Sign up to request clarification or add additional context in comments.

3 Comments

Using = instead of : gives you more freedom over what you can place on the left-hand-side of the statement, but strictly speaking, if you don't use a colon, it's not a type annotation. You could do a,b,c = [float]*3, for example, but any tool designed to monitor type hints (for example a static type checker) wouldn't consider that statement a type hint.
sorry i meant using : still
Ah, ok. (a, b, c) falls into the same camp as a, b, c. It's not an identifier or attributeref or subscription or slicing, so by itself it can't go on the left side of an annotation. You could cheat a little by adding an attribute or subscription to the end of it, for example (a,b,c)[0] : float, but it's not likely that any type hint monitoring tool would interpret this to mean "a, b, and c are floats". And you'd have to assign values to a b and c beforehand, which somewhat defeats the purpose of type hints altogether.
0

It would indeed be nice if PEP484 type-hints supported multiple declarations of same type as a single statement, and also if it supported type-hinting while unwrapping Tuples.

Barring that, I found dummy assignments to be useful as a work-around when wanting to declare multiple variables of the same type in a single statement:

# Multiple type-declarations on same line, but not same statement:
a: List[int]; b: Tuple[float, ...]; c: Iterable[str]
# Multiple declarations of same type in same statement:
# noinspection PyUnusedLocal
k = l = m = n = 0  # type: int
# noinspection PyUnusedLocal
q = r = s = t = ''  # type: str
# noinspection PyUnusedLocal
x = y = z = w = 0.0  # type: float

Explanation: Even if the initial dummy values are never used (because the variables will be overwritten), they still ensure that those variables are understood to have the type of the value first given to them. (I also added old-style comment type hints to make the intention clear to human readers. But removing them makes no difference. PyCharm still understands.)

I am using PyCharm 2021.2.1 with Python 3.8. (PyCharm has its own linter implementing PEP484, separate from mypy.) (Also, I have just begun learning Python this summer, so this answer can probably be improved.)

Comments

-4
 a,b,c: float,float,float

Would be correct

1 Comment

No, this is incorrect. PyCharm gives me two complaints about that code. 1: "End of statement expected: Statement expected, found Py:COMMA" 2: "A variable annotation cannot be combined with tuple unpacking" The "statemen expected" can be fixed with: ​a, b, c: Tuple[float, float, float] But the second issue remains.
-5

You have to assign all the values for all the variables.

E.G:-
a,b,c = 1.0,2.0,3.0
a,b,c = 1.0,1.0,1.0

1 Comment

The question is not about assignment, but type hints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.