Questions tagged [python-3.7]

Version of the Python programming language released in June 27, 2018. For issues that are specific to Python 3.7. Use the more generic [python] and [python-3.x] tags where possible.

Python 3.7 was released on June 27, 2018 (PEP 537).


Use this tag if your question is specifically related to Python 3.7. Otherwise, use the following guidelines to determine which tag(s) you should add to your question:

  • If your question applies to Python in general, use only the tag.
  • If your question applies to Python 3.x but not to Python 2.x, add the tag .
  • If you aren't sure, tag your question with and mention which version you're using in the post.

References

4167 questions
52
votes
5 answers

Why does Python 3 find this ISO8601 date: "2019-04-05T16:55:26Z" invalid?

I supply "2019-04-05T16:55:26Z" to Python 3's datetime.datetime.fromisoformat and get Invalid isoformat string, though the same string works without the Z. ISO8601 allows for the Z - https://en.wikipedia.org/wiki/ISO_8601 $ python3 Python 3.7.2…
stephendwolff
  • 1,382
  • 1
  • 13
  • 27
51
votes
3 answers

“ indirect fixture” error using pytest. What is wrong?

def fatorial(n): if n <= 1: return 1 else: return n*fatorial(n - 1) import pytest @pytest.mark.parametrize("entrada","esperado",[ (0,1), (1,1), (2,2), (3,6), (4,24), (5,120) ]) def…
Laurinda Souza
  • 1,207
  • 4
  • 14
  • 29
48
votes
3 answers

type hint for an instance of a non specific dataclass

I have a function that accepts an instance of any dataclass. what would be an appropriate type hint for it ? haven't found something official in the python documentation this is what I have been doing, but i don't think it's correct from typing…
moshevi
  • 4,999
  • 5
  • 33
  • 50
45
votes
2 answers

How to make "keyword-only" fields with dataclasses?

Since 3.0 there is support to make an argument keyword only: class S3Obj: def __init__(self, bucket, key, *, storage_class='Standard'): self.bucket = bucket self.key = key self.storage_class = storage_class How to get…
wim
  • 338,267
  • 99
  • 616
  • 750
44
votes
2 answers

Python's pathlib get parent's relative path

Consider the following Path: import pathlib path = pathlib.Path(r'C:\users\user1\documents\importantdocuments') How can I extract the exact string documents\importantdocuments from that Path? I know this example looks silly, the real context here…
Mojimi
  • 2,561
  • 9
  • 52
  • 116
38
votes
2 answers

How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?

I'm trying to set up a standard virtual-environment(venv) with python 3.7 on Ubuntu 18.04, with pip (or some way to install packages in the venv). The standard way to install python3.7 seems to be: % sudo apt install python3.7 python3.7-venv %…
GaryO
  • 5,873
  • 1
  • 36
  • 61
35
votes
5 answers

How can I set an attribute in a frozen dataclass custom __init__ method?

I'm trying to build a @dataclass that defines a schema but is not actually instantiated with the given members. (Basically, I'm hijacking the convenient @dataclass syntax for other purposes). This almost does what I want: @dataclass(frozen=True,…
Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
34
votes
2 answers

ImportError: cannot import name 'Literal' from 'typing'

I have recently started using PEP 484 and PEP 586 to make my code clearer and more accessible. So far everything was ok, but when I wanted to use Literal from the package typing it appears it couldn't be imported. What is the most surprising is that…
Kim Vallée
  • 365
  • 1
  • 4
  • 7
32
votes
1 answer

Optional Union in type hint

In the type hint system, Optional[T] is said to be equivalent to Union[T, None] Does this work for multiple type arguments? ie, does Optional[T,U] break out to Union[T,U,None], or do I need to write it as Optional[Union[T,U]]
flakes
  • 21,558
  • 8
  • 41
  • 88
32
votes
3 answers

Python 3.7: check if type annotation is "subclass" of generic

I'm trying to find a reliable / cross-version (3.5+) way of checking whether a type annotation is a "subclass" of a given generic type (i.e. get the generic type out of the type annotation object). On Python 3.5 / 3.6, it works a breeze, as you…
redShadow
  • 6,687
  • 2
  • 31
  • 34
32
votes
5 answers

Replace attributes in Data Class objects

I'd like to replace the attributes of a dataclass instance, analogous to namedtuple._replace(), i.e. making an altered copy of the original object: from dataclasses import dataclass from collections import namedtuple U = namedtuple("U",…
BayerSe
  • 1,101
  • 2
  • 12
  • 23
30
votes
3 answers

How to await a coroutine in pdb

I'm using an async library (asyncpg) and I want to debug some async calls to query the database. I place a pdb breakpoint and want try out a few queries: (pdb) await asyncpg.fetch("select * from foo;") *** SyntaxError: 'await' outside function It…
LondonRob
  • 73,083
  • 37
  • 144
  • 201
30
votes
2 answers

TypeError: Object of type TextIOWrapper is not JSON serializable

If the code was to work properly then whenever someone types something in the chat they get 5 experience and that information gets put into a .json file, but instead what happens is whenever someone types something into the chat it gives me this…
kybt
  • 319
  • 1
  • 3
  • 8
29
votes
4 answers

How to add a dataclass field without annotating the type?

When there is a field in a dataclass for which the type can be anything, how can you omit the annotation? @dataclass class Favs: fav_number: int = 80085 fav_duck = object() fav_word: str = 'potato' It seems the code above doesn't…
wim
  • 338,267
  • 99
  • 616
  • 750
28
votes
1 answer

Python Class "Constants" in Dataclasses

Understanding that the below are not true constants, attempting to follow PEP 8 I'd like to make a "constant" in my @dataclass in Python 3.7. @dataclass class MyClass: data: DataFrame SEED = 8675309 # Jenny's Constant My code used to…
rhaskett
  • 1,864
  • 3
  • 29
  • 48