On Sun, Dec 18, 2022 at 10:23:18PM -0500, David Mertz, Ph.D. wrote:

> I'd agree to "limited", but not "hostile."  Look at the suggestions I
> mentioned: validate, canoncialize, security check.  All of those are
> perfectly fine in `.__new__()`.

No, they aren't perfectly fine, because as soon as you apply any 
operation to your string subclass, you get back a plain vanilla string 
which bypasses your custom `__new__` and so does not perform the 
validation or security check.

> But this much (say with a better validator) gets you static type checking,
> syntax highlighting, and inherent documentation of intent.

Any half-way decent static type-checker will immediately fail as soon as 
you call a method on this html string, because it will know that the 
method returns a vanilla string, not a html string. And that's exactly 
what mypy does:

[steve ~]$ cat static_check_test.py 
class html(str):
    pass

def func(s:html) -> None:
    pass

func(html('').lower())

[steve ~]$ mypy static_check_test.py 
static_check_test.py:7: error: Argument 1 to "func" has incompatible 
type "str"; expected "html"
Found 1 error in 1 file (checked 1 source file)


Same with auto-completion. Either auto-complete will correctly show you 
that what you thought was a html object isn't, and fail to show any 
additional methods you added; or worse, it will wrongly think it is a 
html object when it isn't, and allow you to autocorrect methods that 
don't exist.

_______________________________________________
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/2JPILXSBEPUKHG4E5GH5KJFNOGNWXDYB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to