« first day (4281 days earlier)      last day (650 days later) » 

1:46 AM
https://data.stackexchange.com/stackoverflow/query/1613501/un-deleted-python-questions-closed-as-a-duplicate-of-multiple-other-questions

Thesis: in these cases, either the question should be deleted as Needs More Focus; or the duplicate links need cleanup (a canonical has not been identified)
 
Cool, a new place to lurk
 
(incidentally: 5 is the overall site record for duplicate links on an undeleted question. There is one deleted question each with 7 and 6 such links; the links don't show up properly even if you do have view-deleted privs)
 
 
5 hours later…
6:26 AM
@KarlKnechtel They're both attrocious, TBH.
 
6:38 AM
Lots of bad and broken advice.
 
stackoverflow.com/questions/10149747 Does this question have any value? It seems pretty badly posed - full of misconceptions, too broad, closed with two very different duplicates. It also got used badly as a dupe target once or twice
 
I would actually prefer the current direction. The sleep wording is what I would use when looking for a dupe and there is much less crap advice.
 
fair enough. quantity != quality
but I think I noticed something in the bigger one about how to handle timing across threads as well, which seems like something that's likely to come up
(btw, what time zones are the regulars in around here? when is best to have serious meetings/discussions of a "call to action" sort?)
 
Mostly Eastern Europe and America for the main regulars, though obviously there's a broad representation
As to a Call-To-Arms; the regulars all read the transcript and will see if you have some action you want to take. There could be limited appetite for work on the main site, regardless, but I've explained all that before
 
Sorry, I always get the itches when people start throwing in random modules or related-but-not-quite-the-same things. Sleeping is one of these topics that people can do surprisingly wrong. I am through with the matplotlib (!!!) and similar answers, might look at the threading parade later on.
 
6:50 AM
To be a fly on the wall when MM is trawling threading answers...
 
7:19 AM
could I get one more close vote for a question that was somehow tagged as numpy but not as python stackoverflow.com/questions/72580883
 
you can use the cv tag if you want a cv on a question in chat.
 
I thought that was only in SOCVR, and I didn't go there because the question isn't fresh... but it's probably new enough actually
 
No, we use it here. This is the policy around its use here
 
sorry, i just meant you can use the tag in your message here, instead of having to type out the message. just cv-pls <link>
 
7:46 AM
thanks for the heads-up, both.
 
 
5 hours later…
12:59 PM
how can I type annotate an arbitrarily nested list of integers? [1, 2, [1, 2, [1, 2]]] it can either be int or list[int] and so on, I tried SomeType: typing.TypeAlias = list[int | "SomeType"] but that won't even compile (TypeError)
I came across docs.python.org/3/library/typing.html#typing.ForwardRef but it says "should not be instantiated by a user"
 
yeah, for SomeType = list[int | "SomeType"] mypy says main.py:1: error: Cannot resolve name "SomeType" (possible cyclic definition)
sounds like that data structure is not great for typing, other than "list"
 
I did read mypy does not support recursive types, but my actual code is not running, that is the issue
 
perhaps your actual code has other bugs
 
my entire code is two lines, so I am not sure how it can go wrong anywhere else
the other line being import typing
 
me neither, because you've shown 1
 
1:04 PM
I have now ;)
 
I hope the type error doesn't say something like "type object is not subscriptable"
 
TypeError: unsupported operand type(s) for |: 'type' and 'str'
 
OK, that's not too weird
peps.python.org/pep-0604 is only implemented for types, and a string is not a type
this came up here not long ago, I can try looking it up...
 
thanks, that was what lead me to use typing.ForwardRef("SomeType") the one I linked above
 
Apr 24 at 18:45, by Wes
def func() -> "AAA" | "BBB" | None:
TypeError: unsupported operand type(s) for |: 'str' and 'str'
just kill me already
around here
 
1:08 PM
will check around those texts then
 
list["int | SomeType"] would otherwise probably work
@Jake stackoverflow.com/questions/53845024/… says what you tried is fine, but mypy doesn't understand it yet
"yet" in 2018 so might not be very "yet"
 
maybe I will just try using Unions then, what makes me wonder is typing should not affect the code in anyway right? Python should at least compile
 
we have a few bad exceptions, but yes
if you put these in a dataclass or a singledispatch I bet it will break too
 
huh, SomeType: typing.TypeAlias = list[int | list["SomeType"]] seems to work, mypy complains, but the code runs
based on the question you linked
 
>>> from dataclasses import dataclass
>>> list[int]
list[int]
>>> recintlist = list["int | recintlist"]
>>> @dataclass
... class Foo:
...     bar: float
...     baz: recintlist
...
>>> Foo(42, [1, [3, [4]]])
Foo(bar=42, baz=[1, [3, [4]]])
hmm, it doesn't complain about other inputs
guess I have to look up how dataclasses actually work
 
1:15 PM
dataclasses don't do any validation or conversion or anything of the sort
 
So why the hell do they have type hints wired in?
 
¯\_(ツ)_/¯
 
ugggh
well, at least one less place where type hints affect runtime
 
This is why I keep ranting about dataclasses
 
I thought that was because they were otherwise broken
 
1:17 PM
well you have to define the names somewhere, and this is where you define them? it has to know what is a class variable or not
the annotation is to just to tell that something is a classvar or not? at least that is how I see this
 
singledispatch says, in a 3-level chained exception, TypeError: issubclass() argument 2 cannot be a parameterized generic
>>> isinstance(1, recintlist)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() argument 2 cannot be a parameterized generic
I wonder if that also applies to unions
ah, that's also a parametrized generic
TypeError: Subscripted generics cannot be used with class and instance checks
 
#Perhaps the inventor of dataclasses originally wanted syntax like
@dataclass
class Foo:
    bar
    baz
# ... But that generates a NameError, and modifying Python so it doesn't generate a NameError, would be a big undertaking. So he resorted to type annotation
 
@Kevin assuming from __future__ import annotations we can do bar: _...
spite-driven development
 
Or perhaps instead of speculating about original intentions using my crystal ball, I can just read the PEP
 
"Namespaces are Typing is one honking great idea -- let's do more of those!"
 
1:23 PM
@AndrasDeak--СлаваУкраїні just so I understand this correct, Garthoks = Union[Garthok, Iterable['Garthoks']] replacing Garthok with int does this mean "ints or an iterable of Garthoks (which can be ints or Garthoks themselves)"
 
@Jake I think garthok <-> int, garthoks <-> the type alias you're trying to create
"garthoks" is a garthok or an arbitrarily nested iterable of garthoks
I guess "garthok" <-> list of ints
because you want a list of ints, or an a list of ints inside a list of ints, or ...
The mapping might not be exact, because that question would accept 42, [42], [[42]] etc. but you don't want a bare int
 
so list[int | list["SomeType"]] is what I want instead of my original list[int | "SomeType"]
 
no, like I said, I think your original was correct save the fact that you can't use the pipe like that with a string literal type reference
 
I find this so confusing
 
from my earlier example: recintlist = list["int | recintlist"]
 
1:27 PM
Apr 7 at 19:59, by Kevin
Whenever a typing problem reaches this level of difficulty, I consider it a divine signal that your project has enough type annotation, and you're allowed to stop
 
This is a list with ints or recintlists inside. Each "recintlist inside" is a list with ints or recintlists inside.
 
lmao, I guess I will just go with Andras' pipe in a string solution or just SomeType: typing.TypeAlias = list[typing.Union[int, "SomeType"]]
 
Angels descending from on high, playing a chorus of "Time To Work On Something Else (in D Minor)"
 
@Jake yes, the two are the same, you just have to choose which ugly you like better
Does the TypeAlias hint do anything there?
 
not really, its a type hint that I wanted to sneak in
 
1:31 PM
Does it really not do anything? Because then you're just choosing to write unreadable code.
 
you do have a point, at least I do not see a need for it now, there is an entire PEP for it after all PEP 613
 
"The thing exists" and "the thing has a function in the place you are using it" are two very different things. I'm not familiar with it at all, which is why I'm asking if it does anything. If it does: learn what it is. If it doesn't: remove it.
OK, reading that PEP it's clear that it's way above my typing pay grade.
 
premise 1: Don't write code that doesn't do anything.
premise 2: Type annotations don't do anything.
Conclusion: Don't write type annotations.
 
I glanced over the PEP, turns out it is there to make type aliases explicit? that is my takeaway, and I am lower than your pay grade
 
MyType = "ClassName"
def foo() -> MyType: ...

# This code snippet should not error so long as ClassName is defined later on. However, a type checker is forced to assume that MyType is a value assignment rather than a type alias, and therefore may throw spurious errors that
# (1) MyType is an unannotated global string, and
# (2) MyType cannot be used as a return annotation because it is not a valid type.
sounds like this is all about fixing assumptions made by type checkers
 
1:38 PM
@Kevin I will just dump this, the only reason I even knew TypeAlias was the existing code base had this, we hired a python consultant who was supposed to make things better and he added these
 
I would very much expect the above to be the exact same as def foo() -> "ClassName": ... (meaning my expectations don't match that of the python typing ecosystem)
Also, why does MyType = "ClassName" raise about "an unannotated global string"? How much type annotation does a string literal need?
 
it can be str or Literal[str]?
 
# v---- str
MyType: str = cast(str, "ClassName")  # <-- str
# ^---- str
perfect
 
I am missing something, what is this supposed to show?
 
the ideal amount of annotation for a string literal :P
nothing, really, I'm just complainign because I don't know much typing and it seems really really unintuitive most of the time
 
1:43 PM
Is Literal really a type annotation? Seems a bit silly because it's not so much a type as it is an element of the Python grammar
 
@Kevin it is, typing.Literal
 
tbh you can swap Literal[str] with string Enums and it would be a better alternative IMO
 
Python knows which of your expressions are string literals, because they're called StringLiteral in the AST. No annotation required
 
@Kevin my impression is that typing literalness is about specifying more than the type: specifying an instance you could compare against to. So Literal[True] rather than bool if you know it will always be True.
Similarly Literal['foo'] if you know the string is the string 'foo'. I assume. But see my recent message about intuitivity.
 
(To well-actually myself, the real name of the ast node is Constant)
There may be some practical utility in annotating a string object as "constructed exclusively from values known at compile time". Compared to, say, a string that the user provided, or something dynamically generated from external data. But I wouldn't use the term "Literal" to make a distinction like that.
 
1:49 PM
@Kevin but this seems different to me from the concern of having a variable that is very obviously a string literal. It's right there in its literalness.
I assume there's some subtlety here about typing semantics.
 
Currently reading peps.python.org/pep-0586... I'm reminded of Aran-Fey's question the other day about open(filename, "rb"). The PEP is arguing that "r" and "rb" are Literals, in a conceptual sense.
 
that looks just like what I said: specifying specific values beyond the type
Arguably this isn't even typing's turf, but of course it's pragmatic for this to be checked.
 
Having read the document, I will change my rating of Literal from "silly" to "a little silly". As you say, it's not really typing's turf.
 
so have you reached enlightenment on whether and how the following two are different?
foo: str = 'literal here'
foo: Literal['literal here'] = 'literal here'
other than the hilarious duplication in the latter
and if and how either of these are different from foo = 'literal here'
it would probably be easier if we weren't a bunch of blinds leading each other, but it's more fun this way
 
2:06 PM
I'd say the difference is: what promises you're making about assignments you'll perform on foo in the future. foo: str = 'literal here' promises "I will only assign string objects to this later". foo: Literal['literal here'] = 'literal here' promises "I will only assign exactly the value 'literal here' to this later". foo = 'literal here' promises nothing at all.
 
apparently "blind" only works as an adjective
 
The only blinds-as-nouns I know of are venetian blinds and duck blinds.
 
Strictly speaking, it's impossible to infer the type of a variable, so foo = 'literal here' might be str or Iterable[str] or Sequence or Container or 3 dozen other things
In practice it doesn't matter unless you reassign foo sometime later
 
@Kevin the wiktionary has some other examples but yeah
 
foo: Literal['literal here'] = 'literal here' is pretty silly at face value, but it becomes more useful when you have a larger set of possible values. like mode: Literal["r", "w", "a"] = "r".
For much the same reason that a list isn't very useful if it's guaranteed to have a length of one at all times
 
2:11 PM
is there a good resource anyone would suggest so I can understand typing?
I have been reading (this chat) and I can hardly keep up
 
@Jake that's partly because neither Kevin nor I are good at it
 
If my words seem confusing, it's probably because I myself am confused.
 
Even a single-option Literal can still make sense though:
s: str = 'hi'
t: Literal['hi'] = 'hi'

y: Literal['hi', 'bye']
y = s  # error
y = t  # fine
 
yeah, I just tested this on the mypy playground:
from typing import Literal

literally: Literal['lit'] = 'lit'
figuratively = 'lit'

def foo(arg: Literal['fig', 'lit']): pass

foo(literally)  # works
foo(figuratively)  # fails
 
Also consider Final for certain use cases involving values that shouldn't change
In a perfect world, I would like mypy to permit both of those calls. But in the real world, they're operating ten feet away from the bottomless pit known as the Halting Problem, so I'll give them some slack when they're overly fussy.
figuratively: Final = 'lit' makes mypy happy, for the record
 
2:57 PM
brief cbg all
 
3:41 PM
cbg
 
3:53 PM
cbg
 
4:33 PM
@Jake You need to quote the entire type, i.e. SomeType: typing.TypeAlias = "list[int | SomeType]". That won't fix the problem of MyPy not supporting cyclic types (yet), but it is the proper declaration. Mixing quoted and unquoted types has no benefit whatsoever.
@AndrasDeak--СлаваУкраїні In principle, a type is more than a class. Rather it's a "group" of values. So the type bool represents the values {True, False}; in that regard, types can also represent specific values or even no values.
 
@MisterMiyagi noted
@MisterMiyagi also noted, thanks
 
I also have some nice async bridges to sell, in case you are interested.
 
who wouldn't want one of those? I'm in :p
 
4:49 PM
hello guys how are yall
 
@JonClements That's
@MisterMiyagi the spirit!
 
The other week when I was trying to reinvent all binary operators from scratch, I noticed that none of my axioms directly implied that there are only two boolean values. It doesn't matter that every combination of and/or/not/true/false always gives either true or false -- there may be more exotic values unreachable from our starting point.
Maybe there are values {strange, charm}, such that "not(strange) = charm" and "strange + charm = strange", and "strange | charm = charm". Now our boolean operators are defined for four values rather than two.
This is about as useful as observing that the imaginary number line behaves just like the real number line when performing addition and subtraction. It's not particularly interesting to say that 2j +2j = 4j.
 
Yeah, you need tertium non datur
 
@AndrasDeak--СлаваУкраїні A fine addition to my collection of latin wossnames, thank you
It's the sine qua non of my nom de plume
 
et cetera... et cetera... ?
 
5:04 PM
@Kevin latin as in latin1
 
DNA testing of the words indicates at least 1% latin heritage
I think I did need something like the law of excluded middle when I was reinventing binary logic. Intuitively, I knew that "(A implies B) and (not(A) implies B)" was sufficient to prove B, but I couldn't get there by applying the axioms and theorems I had collected. But maybe if I also had an axiom declaring that {A, not(A)} were the only two possible values...
 
 
1 hour later…
6:13 PM
Cbg
 
cbg
@The_Bread_Coder Hello :) Sorry you didn't get a response, I think people were engrossed in another discussion
@MisterMiyagi I haven't entirely got my head around typing (and I don't think I care to after observing discussions here) but one thing in Rust that I find odd is the type of statement - "Because X is Copy" or "As Y is Z". Is that roughly what MyPy is shooting for, or are they unrelated?
I've kinda got used to it in those contexts, as it really is fundamental and definitive in the language. If it's that kind of strictness that they're trying to backport to Python then it probably explains why I don't understand half of the things that keep coming up in discussions about typing here
I don't even think I've seen that kind of language being thrown around frequently in Java answers, but then I'm usually completely confused about what the hell is going on at all so I might not notice the slightly odd turn of phrase
 
6:40 PM
I've just read Wikipedia's description of rust typing, and I don't see any magic that would make "X is Copy" a sensical statement
Unless it's using is in exactly the way that Python uses it, in which case you could say that X is Copy right after you run the statement X = Copy
> Generics in Rust use trait bounds for their generic parameters, which precisely define what is required of a type in order to be used with a given generic function.
Maybe this is related.
Perhaps there are scenarios where a generic uses an interface that's exactly the same as the public methods/properties of a concrete type. If frobnicate(T) accepts any object that implements __add__ and __div__ and __repr__, and the int type implements exactly and only __add__ and __div__ and __repr__, maybe it's meaningful to say that int is T.
 
6:57 PM
It's even in their docs - "String is Clone but not Copy"
I wondered whether there might be some strict CS type theory that I was missing
 
I see something similar in doc.rust-lang.org/book/…. "X is Y" seems to be equivalent to "the type X implements the interface described by Y"
 
7:21 PM
@roganjosh I would guess it's a shorthand for the "is a" relation commonly used for types, which is basically "isinstance" and "issubclass" rolled into one.
 
Makes sense, I just think part of my misunderstanding in python might be because this is being retroactively applied to python, which I never really learned in this context. Maybe I should pay more attention to the discussions about it, but at least half of them seem to end in frustration
 
I wonder if Rust has any magic that makes it easy to define traits after you define a type that might qualify for that trait. For example, maybe I would define a type Dog with the method bark, and later define a Trait as "a type is Loud if it implements bark". Since Rust is compiled and strongly typed and strictly typed(?), it should be able to figure out which existing types are Loud.
 
This exchange made me chuckle:
6 hours ago, by Andras Deak -- Слава Україні
So why the hell do they have type hints wired in?
 
Python's type system is a strange beast compared to, say, Rust because on the one hand runtime types are things - e.g. passing around the int type as an object - but at the same time static types are completely inert - e.g. you cannot say X. zero() even when X must be a number - but there are some intermediate beasts like Union that are sometimes concrete.
 
@Kevin I don't think it does that but I couldn't be certain
Or, rather, that it can do that
 
7:29 PM
I suspect that trait-oriented design is easier to make memory-safe than type-oriented design. No, I can't explain what I mean by that.
 
IIRC the thing about traits is that trait "membership" is defined separately of the type, so you can extend the type with extra traits or vice versa. Can't do that if you have to list all interfaces when defining the type.
 
I also don't think I'd want it to do that. In an only-slightly-hypothetical-case I might specify two objects with methods called find_deliveries() and run(), yet one would be in a ruin step (to remove jobs from a solution) and the other in a recreate step that inserts jobs back into a solution. I want that to be explicit
 
There's also the very nice feature that you can decide which trait you are using now, so the same type may provide Tree.bark and Dog.bark separately.
Just in case you ever wanted a treedog as a pet.
 
If a treedog rolls over and nobody is there to see it... does it bark?
I believe I've just advanced the field of philosophy immeasurably with that one
 
Guess we'll never know until some madman creates a treedog. exits stage left
 
 
1 hour later…
9:00 PM
How can I output from a logger the name of the function from which the logging message is being sent? Like, if I have a function foo, and I want to send a logging.INFO from it, I could do something like logging.info("Hello world"), but how would I make the output also tell me that the message came from foo without simply writing it in the message? Similar to how standard python errors will tell you which function the error is coming from
 
>>> def foo(): pass

>>> foo.__name__
'foo'
but the last part sounds like you're looking for a traceback docs.python.org/3/library/traceback.html
 
Thank you both
 
 
2 hours later…
10:46 PM
https://stackoverflow.com/questions/72890359

Is there a canonical for "`list[int]` doesn't work as-is in 3.7 as the feature wasn't available yet; use `typing.List[int]`"? The closest I can find is https://stackoverflow.com/questions/62871524, which seems problematic as a Stack Overflow question (should be reworked into a q/a pair, and not read like a rant)
 
10:57 PM
and its dupe targets, obviously
 
11:24 PM
Between stackoverflow.com/questions/37087457 and stackoverflow.com/questions/39458193, which seems better as a canonical? (The top answers are written by the same person!)
 
Martijn is a well-known moderator as well as Python guru, also one of the room owners here.
 

« first day (4281 days earlier)      last day (650 days later) »