Questions tagged [python-3.10]

A version of the Python programming language, first released on October 4, 2021. This tag is for issues that are specific to Python 3.10. For general questions use the more generic [python] tags.

Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.

References

782 questions
91
votes
3 answers

An error while trying to execute tests on python 3.10 with pytest

If I try to execute tests using pytest with python 3.10 I got the following error: TypeError: required field "lineno" missing from alias Google says it's a known problem, but I found no way to solve it. Tests start fine if I launch them with 3.9
chydik
  • 1,037
  • 1
  • 7
  • 5
79
votes
1 answer

ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)

I use virtualenv to create a python virtual environment for my python project. #command pwd #result /home/dhanusha/Documents/projects/my-project # command virtualenv --version # results virtualenv 20.10.0 from…
Dhanusha_Perera07
  • 3,347
  • 5
  • 14
  • 22
72
votes
6 answers

How to use values stored in variables as case patterns?

I'm trying to understand the new structural pattern matching syntax in Python 3.10. I understand that it is possible to match on literal values like this: def handle(retcode): match retcode: case 200: print('success') …
61
votes
2 answers

How to use multiple cases in Match (switch in other languages) cases in Python 3.10

I am trying to use multiple cases in a function similar to the one shown below so that I can be able to execute multiple cases using match cases in python 3.10 def sayHi(name): match name: case ['Egide', 'Eric']: return f"Hi…
Egidius
  • 971
  • 1
  • 10
  • 22
58
votes
11 answers

in VS Code ImportError: cannot import name 'Mapping' from 'collections'

I am trying to connect to Postgress and create a folder test.db via Flask. When I run "python3" in the terminal and from there when I run "from app import db" I get an import error: ImportError: cannot import name 'Mapping' from 'collections'…
Mitra
  • 591
  • 1
  • 3
  • 7
52
votes
4 answers

Python 3.10+: Optional[Type] or Type | None

Now that Python 3.10 has been released, is there any preference when indicating that a parameter or returned value might be optional, i.e., can be None. So what is preferred: Option 1: def f(parameter: Optional[int]) -> Optional[str]: Option 2: def…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
37
votes
3 answers

Is there a way to match inequalities in Python ≥ 3.10?

The new structural pattern matching feature in Python 3.10 is a very welcome feature. Is there a way to match inequalities using this statement? Prototype example: match a: case < 42: print('Less') case == 42: print('The…
gustafbstrom
  • 1,622
  • 4
  • 25
  • 44
35
votes
3 answers

How to do an else (default) in match-case?

Python recently has released match-case in version 3.10. The question is how can we do a default case in Python? I can do if/elif but don't know how to do else. Below is the code: x = "hello" match x: case "hi": print(x) case "hey": …
farhan jatt
  • 509
  • 1
  • 8
  • 31
27
votes
1 answer

Python 3.10 pattern matching (PEP 634) - wildcard in string

I got a large list of JSON objects that I want to parse depending on the start of one of the keys, and just wildcard the rest. A lot of the keys are similar, like "matchme-foo" and "matchme-bar". There is a builtin wildcard, but it is only used for…
27
votes
4 answers

NumPy 1.21.2 may not yet support Python 3.10

Python 3.10 is released and when I try to install NumPy it gives me this: NumPy 1.21.2 may not yet support Python 3.10.. what should I do?
Mohamed Yahya
  • 473
  • 1
  • 4
  • 13
27
votes
2 answers

Capture makes remaining patterns unreachable

Why does this code fail: OKAY = 200 NOT_FOUND = 404 INTERNAL_SERVER_ERROR = 500 match status: case OKAY: print('It worked') case NOT_FOUND: print('Unknown') case INTERNAL_SERVER_ERROR: print('Out of service') …
27
votes
2 answers

Convert multiple isinstance checks to structural pattern matching

I want to convert this existing code to use pattern matching: if isinstance(x, int): pass elif isinstance(x, str): x = int(x) elif isinstance(x, (float, Decimal)): x = round(x) else: raise TypeError('Unsupported type') How do you…
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
22
votes
2 answers

Python 3.11 worse optimized than 3.10?

I run this simple loop with Python 3.10.7 and 3.11.0 on Windows 10. import time a = 'a' start = time.time() for _ in range(1000000): a += 'a' end = time.time() print(a[:5], (end-start) * 1000) The older version executes in 187ms, Python 3.11…
Michael
  • 878
  • 5
  • 17
19
votes
4 answers

Structural pattern matching using regex

I have a string that I'm trying to validate against a few regex patterns and I was hoping since Pattern matching is available in 3.10, I might be able to use that instead of creating an if-else block. Consider a string 'validateString' with possible…
18
votes
1 answer

New union shorthand giving "unsupported operand type(s) for |: 'str' and 'type'"

Before 3.10, I was using Union to create union parameter annotations: from typing import Union class Vector: def __mul__(self, other: Union["Vector", float]): pass Now, when I use the new union shorthand syntax: class Vector: def…
rencedm112
  • 397
  • 3
  • 11
1
2 3
51 52