As one of those who 1. thinks there IS a problem, 2. that the problem is bigger than most of the people on this thread seem to think, I am nevertheless in agreement that tackling the problem by changing the language to outlaw direct str iteration would just be far, far too disruptive. I am much more receptive to the idea of adding a totally separate atomic string.
> I agree with the numerous posters who have brought up the > backward-compatibility concern. This change *would* break lots of code. At > the same time, this bites me consistently, so I'd like to do something > soon... at least sooner than 6.0 ;). > > I believe that this is better solved by static analysis. I suggested some > time ago on typing-sig that we explore adding a `Chr` type to typing, and > type `str` as a `Sequence[Chr]` rather than a `Sequence[str]`. You can read > the proposal here (it's not very complex at all, and should be > backward-compatible for all but the hairiest cases, which just need either > a cast or an annotation): > > > https://mail.python.org/archives/list/typing-...@python.org/thread/OLCQHSNCLM2TOHRQWB3WPSI66AA5A3G7 > > With it, we have a path forward where type-checkers like mypy assure us > that we're really doing what we think we're doing with that string, and > require explicit annotations or casts for the ambiguous cases. That > discussion fizzled out, but I'm still very much interested in exploring the > idea if it seems like a realistic alternative. I think it makes much more > sense than changing the mostly-sensible, well-known, often-used runtime > behavior of strings. > > Brandt > Also as a person who has really come to LOVE static typing in Python since it has saved me literally HOURS of debugging the (awful) code I tend to write (mostly in PyCharm actually; haven't made much use of mypy yet), tackling this issue via mypy and static analysis very much sounds, to me, like a great way to go. If there were a Chr type with which to statically type against in the manner of Sequence[Chr], and also perhaps an "AtomicString" static type that does nothing but disallow Sequence-type behavior (iteration and slicing) in static analysis (but could still be type cast to regular old str), I sure would use the heck out of it. The type/method warnings in Pycharm would solve ALL of the problems I have run into over the years- I would no longer need to write non-idiomatic, unpythonic guarding code trying to stop myself from sending strings into functions meant for non-str iterables of strings only.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/PEZSENL5DC34U2OC57ZL66VPEOISYW6Q/ Code of Conduct: http://python.org/psf/codeofconduct/