« first day (4876 days earlier)      last day (285 days later) » 

6:34 AM
@smci well I was writing a django admin command. However for that command I wrote some utility function like "kill_all_session(expire_date, ignore_active)" - and I was wondering, that function is not using "self", so at least should be static. But is it even part of the "cleanup" command class?
6:58 AM
@smci I mean, I wouldn't want to discriminate... :P But no, that's not the core audience
8:29 AM
@paul23 If you're not using cls or self, top-level function should be the default, and staticmethods the exception, if it really doesn't make sense for it to be available outside of the class. Since you're wondering that yourself, I'd go with top-level.
 
2 hours later…
10:38 AM
inside my chatbot i have a condition for exit, which function can i call to exit my python script?
Hello, what can i do when the code in one question is more than the body limit? (30,000 characters)
Can i post a link in github?
Work on Minimal part of Minimal Reproducible Example
I doubt you will get much answers for 30000 characters dump of code in question.
 
5 hours later…
3:40 PM
Is there some sort of design pattern for when you have a nested data structure and you want to change the type of a nested object? Simplified example: I have a Settings class with an attribute column_labels: list[str | None]. I then replace all the Nones with strings. But of course the type checker still thinks that the list might contain Nones. Is there an elegant way to make this type safe?
class Settings:
    csv_file_path: Path
    column_labels: list[str | None]
    ...  # Lots of other stuff

def do_stuff(settings: Settings):
    df = pd.load_csv(settings.csv_file_path)
    settings.column_labels = [
        label1 or label2
        for label1, label2 in zip(settings.column_labels, df.columns)
    ]

    # Error, type checker doesn't understand that it's now a list[str]
    label: str = settings.column_labels[0]
What I've done in the past is 1. rename column_lables to be private/hidden/distinct. 2. add a functools.cached_property which only returns strings. However, the approach probably won't work with what you're doing. I haven't been able to come up with an approach (other than creating a new Settings type) which can do what you want.
I think partial types may work. But I can't remember if people are for/against/bikeshedding the concept in Python.
I feel like that goes against the point of typing. What if you don't run do_stuff?
I know it's not clean to mutate the settings object, I only wrote it that way because it's short and simple. The bottom line is that do_stuff needs to turn a list of str | None into a list of str
You can use typing.cast to force different type
@matszwecja You can probably use a TypeGuard to more 'typing'
to be more*
3:53 PM
cast doesn't work on object attributes though
If I make two separate classes, is there a nice way to convert one to the other? Best I can think of is
class _BaseSettings:
    csv_file_path: Path
    ...  # Lots of other stuff

class Settings(_BaseSettings):
    column_labels: list[str | None]

class ProcessedSettings(_BaseSettings):
    column_labels: list[str]

def do_stuff(settings: Settings):
    processed_settings = ProcessedSettings(**vars(settings))
    processed_settings.column_labels = [
        label or 'foo' for label in settings.column_labels
    ]
Ugh, I forgot TypeGuard doesn't work on self but:
from __future__ import annotations
from typing import TypeGuard

class Settings:
    column_labels: list[str | None]
    @staticmethod
    def with_column_labels(self: Settings, labels: list[str]) -> TypeGuard[SettingsStr]:
        self.column_labels = [
            label1 or label2
            for label1, label2 in zip(self.column_labels, labels)
        ]
        return True

class SettingsStr:
    column_labels: list[str]

def do_stuff(settings: Settings):
    df = pd.load_csv(settings.csv_file_path)
Works in VS Code for me YMMV
Hmm. I'm not quite sure how you'd add all the remaining attributes to SettingsStr there
I'd do something like you did but make everything except the 'real Settings class' a Protocol.
4:08 PM
But how would you define SettingsStr without having to duplicate all the ... # Lots of other stuff?
From my eyes your ProcessedSettings perfectly solves the duplication problem. I must be misunderstanding something.
Well, ProcessedSettings can't inherit from Protocol and _BaseSettings, that's not allowed
Why can't _BaseSettings be a Protocol too?
Not entirely sure why, but pyright thinks Settings can't be instantiated if I do that
Wat. Is pyright the one used in pylance (VS Code) or another one?
4:21 PM
Pyright is the one in vscode, yeah
I've merged our two solutions together to get: pastebin.com/xT89mDs0
Which other than the self in the staticmethod types fine for me in VS Code strict mode. Is my stuff out of date again with breaking changes?
Is there a way to get rid of the assertion with side effects?
Oh, and without having to manually write the constructor for Settings
For the side effects I'm not sure. Possibly if partial types get added to Python, but I can't think of another way rn. Other than the obvious cast and # type: ignore. As for __init__ I normally use dataclasses when using the approach you can probably do so here.

« first day (4876 days earlier)      last day (285 days later) »