News for hackers. Stuff that matters.
Re:Effect of Perceptual Load on Performance Within IDE in Pe... by dagmx on Friday July 14, 2023 @04:11PM (#36729290)
Your code is not what I’m describing at all. You’re just talking past me without actually trying to understand what I’m saying or reading the specifics.

A Python instance is effectively a dictionary. The contents of that dictionary can be mutated at runtime to disagree with the type that is specified. This is perhaps an antipattern but it’s not uncommon.

Attribute lookups can be dynamic as well without any typing. The typing can be lofted out and specified at the lookup site but that is also no guarantee of what you’ll get at runtime.

You cannot type check this dunder method access.

class A(dict):

    def __getattr__(self, name):

       return self[name]
You may say “oh but that’s uncommon or bad practice” but it’s unfortunately quite common in many production systems because Python has encouraged embracing its dynamicism for decades. That’s not a flaw, that’s a power. But it definitely has its pros and cons.

One may also say that “well that’s just void pointer casting in C or other languages”, which is also true and equally problematic. That’s why newer compiled languages like Rust and Swift have options for sum types / enums that carry a value to design around it in a type safe manner.

———

To your other point on courtesy…

If I say you’re unfamiliar with the details, it’s because you yourself said you aren’t sure YET you claim your world view of type checking is correct and dynamicism is rare. You don’t read my comments and substitute your own.

If I am being rude it is only because your default world view appears to be “the ideal in my head is the way it is”.

Take for example when you say “your rebuttal almost never occurs” or when you say pretty much every tech company uses type checking. You ascribe your own world view with no room for greys or real world variance.

If I say: “but this is my experience” your response is “well that’s not the reality for most cases”. So if I am rude it is because you talk in absolutes and only correct yourself when pushed back upon.

Perhaps do not talk so assuredly in general. Your experiences are not that of others yet you talk as if they’re the norm.

So if I am rude, it is because you are being rude in your responses and perhaps don’t realize it. You’ve basically told everyone who’s responded to you that their experiences aren’t correct.

Anyway I shall not respond further to you because this discussion is hyper fixating on something that was only intended to be an example. It serves no purpose to continue arguing

Re:Effect of Perceptual Load on Performance Within IDE in Pe... by formulathree on Friday July 14, 2023 @04:56PM (#36729828)
>A Python instance is effectively a dictionary. The contents of that dictionary can be mutated at runtime to disagree with the type that is specified. This is perhaps an antipattern but it’s not uncommon.

Yes I know. but the you will see the code addresses this. Mypy caught the monkey patching. Look at it. I added a property to an instance dynamically and mypy caught it.

>You may say “oh but that’s uncommon or bad practice” but it’s unfortunately quite common in many production systems because Python has encouraged embracing its dynamicism for decades. That’s not a flaw, that’s a power. But it definitely has its pros and cons.

I addressed this in the post you replied to. I specifically stated it. Let me quote it:

"__getattr__ is type checked too as long as you type the method signature, I have seen some weirdness around this area for type checkers but that's not the main problem. I understand where you are getting at with __getattr__. When you use this you're essentially creating something akin to a function with a string as a parameter. The contents of a string can't be type checked and if all methods are defined this way on a class none of it can be checked."

You addressed a point I already addressed, and didn't address the points you were factually wrong about: Dictionary access is type checked. Monkey patching is also type checked.

>Anyway I shall not respond further to you because this discussion is hyper fixating on something that was only intended to be an example. It serves no purpose to continue arguing

Completely agreed let's move on from all the BS. But you said some things that were factually wrong about python Dicts not being type checked. You said dictionary access is not type checked. Please address those things. I am interested in your viewpoint on this matter because I can learn something if my statement is wrong.

Re:Effect of Perceptual Load on Performance Within IDE in Pe... by still_grokking on Friday July 14, 2023 @09:13PM (#36732117)
>> A Python instance is effectively a dictionary. The contents of that dictionary can be mutated at runtime to disagree with the type that is specified. This is perhaps an antipattern but it’s not uncommon.

> Yes I know.

I'm not sure…

> but the you will see the code addresses this. Mypy caught the monkey patching.

LOL. Here, what "static type checking" is worth in Python in reality:

  me@box:~$ cat test.py 
  class A(dict[int, float]):
      def __init__(self) -> None:
          self.__dict__["you_are_clueless"] = "Oh boy, so clueless…"
      
      def __getattr__(self, name: str) -> int:
          result: int = self.__dict__[name+"_clueless"]
          return result
  
  some_a = A()
  print(some_a.you_are_)


  me@box:~$ mypy --strict test.py 
  Success: no issues found in 1 source file


  me@box:~$ python test.py 
  Oh boy, so clueless…
  Traceback (most recent call last):
    File "/home/me/test.py", line 12, in <module>
      print(some_a.you_are_)
            ^^^^^^^^^^^^^^^
    File "/home/me/test.py", line 6, in __getattr__
      result: int = self.__dict__[name+"_clueless"]
                    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  KeyError: 'you_are__clueless'

Class `A` claims to be a Dict from `int` to `float`. But I can freely assign a string to a string key…

The signature of `__getattr__` makes no sense in this context, but is happily accepted…

In the body of said function I'm obviously assigning a string to an `int` "typed" variable. But no issue here…

I'm returning this string in contrast to what the signature claims. But also no issue here…

I'm calling an non-existent method on the `A` instance. But that's not an issues for "type checking"…

In comparison, a language with real static type checking:

  class Record(fields: (String, String)*) extends Selectable:
    private val fieldMap = fields.toMap
    def selectDynamic(key: String): String = fieldMap(key)
    //def selectDynamic(key: String): Int = fieldMap(key)
    // >> Compile error:
    //   Found:    String
    //   Required: Int
  
  
  type A = Record { val youAreClueless: String }
  val someA = Record("youAreClueless" -> "Oh boy, so clueless…").asInstanceOf[A]
  
  println(someA.youAreClueless)
  
  //println(someA.youAre_Clueless)
  // >> Compile error:
  //   value youAre_Clueless is not a member of Playground.A
  
  //val someB = Record(123 -> 0.987).asInstanceOf[A]
  // >> Compile error:
  //   Found:    (Int, Double)
  //   Required: (String, String)
It's not even possible to replicate here the rest of the nonsense Mypy is doing above.

[ Live Scala code at: https://scastie.scala-lang.org/eGVUZbVARMuZa36ZyLQqxQ ]

Note, this Scala demo is purely static. The field access is checked at compile time.

Caveat: The code isn't completely safe, as this construct could possibly explode at runtime when doing something like:

  val someB = Record("foo" -> "0.987").asInstanceOf[A]
  println(someB.youAreClueless)
This compiles but ends up in a runtime exception `NoSuchElementException: key not found: youAreClueless`.

But if I wouldn't be lazy I could even get rid of the cast to A (`asInstanceOf[A]`) which is what makes the code unsafe. This would require meta-programming, though, and would be more involved (and therefore likely quite hard to follow if you don't know the language). The point is: It could be made 100% compile time safe! One "just" needs a powerful language with a proper type system.